SlideShare a Scribd company logo
1 of 26
openFrameworks
     events
Events
• Send notifications on certain events or create
  your own.

• Wraps around Poco Events system
• You register for the events you want to receive.
• testApp receives events automatically (       ,
                                         keyPressed
  keyReleased, mouseMoved)
Events
Delegates
• A target subscribes to an event by registering a
  delegate.

• You can see the delegate as an “Event listener”
• An event has always one argument.
• There is one global ofEvents object which holds
    the OF related, event instances to which you can
    subscribe.

• OF uses a First In First Out (FIFO) event queue.
Events
Listen for OF-events
Create a function which is called when the event
notification is sent. This function must have one
argument of the correct event type.

Use ofAddListener(event object, callback object,
callback function) to tell the event system you want
to listen for events.

You can remove your regiseterd listener by calling
ofRemoveListener(event object, callback object,
callback function)
Register to an event
1. Decide to what event you want to listen
See events/ofEvents.h for the available OF events
in the ofCoreEvent class.
For example:
ofEvent<ofMouseEventArgs> mouseMoved

The name of the event is mouseMoved and passes
am ofMouseEventArgs to the callback.
Register to an event

                                                       Name of event is
class ofCoreEvents {
 public:                                               mouseMoved which

 ofEvent<ofEventArgs> 
 

 ofEvent<ofEventArgs> 
 
                                setup;
                                update;                stores the listeners

 ofEvent<ofEventArgs> 
 
      draw;

 ofEvent<ofEventArgs> 
 
      exit;

 ofEvent<ofResizeEventArgs>    
 indowResized;
                                w


   ofEvent<ofKeyEventArgs> 
   keyPressed;

   ofEvent<ofKeyEventArgs> 
   keyReleased;


   ofEvent<ofMouseEventArgs>   
   mouseMoved;

   ofEvent<ofMouseEventArgs>   
   mouseDragged;

   ofEvent<ofMouseEventArgs>   
   mousePressed;    ofMouseEventArgs is

   ofEvent<ofMouseEventArgs>   
   mouseReleased;

   ...                                              passed as parameter to
}
                                                     the callback
Register to an event
2. Create the callback function
Create a function which receives the event type as
first and only argument.
ofEvent<ofMouseEventArgs> mouseMoved

      class testApp : public ofBaseApp{
      
 public:
      
 
      void myCustomMouseReleased(ofMouseEventArgs& args) {
      
 
      
   cout << "received a mouse released event" << endl;
      
 
      
   cout << "mouse x:" << args.x << endl;
      
 
      
   cout << "mouse y:" << args.y << endl;
      
 
      
   cout << "mouse button:" << args.button << endl;
      
 
      }
      }
Register to an event
3. Tell the event object you want to listen
Use the global function ofAddListener to register
your new function as a callback for the event. Note
that ofEvents is the global instance of ofCoreEvents


 void testApp::setup(){
 
 ofAddListener(
 
 
      ofEvents.mouseReleased 
 
  
   // the event object
          ,this 
 
   
  
   
  
  
  
   // testApp pointer
 
 
      ,&testApp::myCustomMouseReleased
   // the callback function.
 
 );
 }
UnRegister from event
To unregister from an event, you simply call
ofRemoveListener() with the same parameters
when you added the listener.

You can re-enable the listener again by
calling ofAddListener() again.

            void testApp::setup(){
            
 ofRemoveListener(
            
 
      ofEvents.mouseReleased
            
 
      ,this
            
 
      ,&testApp::myCustomMouseReleased
            
 );
            }
Recap event listening
Listen for OF-events
1. Create listener function
2. Find the event you want to listen to.
3. Call ofAddListener() to register


Remove listener
1. Call ofRemoveListener()
Event listening
Register to all mouse events
Create these functions:
void mouseDragged(ofMouseEventArgs& args)
void mouseMoved(ofMouseEventArgs& args)
void mousePressed(ofMouseEventArgs& args)
void mouseReleased(ofMouseEventArgs& args)

Call ofRegisterMouseEvents(this) to
register to all mouse events. Instead of
registering for each one separately. To unregister
call ofUnregisterMouseEvents(this)
Event listening
class MyWorker {
public:

 MyWorker() {

 }


 void setup() {

 
     ofRegisterMouseEvents(this);

 }


 void mouseMoved(ofMouseEventArgs& args) {

 
     cout << "Moved: " << args.x << ", " << args.y << endl;

 }


 void mouseDragged(ofMouseEventArgs& args) {

 }


 void mousePressed(ofMouseEventArgs& args) {

 }


 void mouseReleased(ofMouseEventArgs& args) {

 }

};
Event listening
Register to all keyboard events
Create these functions:
void keyPressed(ofKeyEventArgs& args)
void keyReleased(ofKeyEventArgs& args)

Call ofRegisterKeyEvents(this) to
register to all key events. To unregister
call ofUnregisterKeyEvents(this)
Event listening
Register to all touch events
Create these functions:
void touchDoubleTap(ofTouchEventArgs& args)
void touchDown(ofTouchEventArgs& args)
void touchMoved(ofTouchEventArgs& args)
void touchUp(ofTouchEventArgs& args)
void touchCancelled(ofTouchEventArgs& args)

Call ofRegisterTouchEvents(this) to
register to all touch events. Unregister using
ofUnregisterTouchEvents(this)
Event listening
Register to all drag events
Create these functions:
void dragEvent(ofDragInfo& args)

Call ofRegisterDragEvents(this) to
register to all drag events. Unregister
with ofUnregisterDragEvents(this)
Simple messaging
openFrameworks adds a very simple way to send
messages between objects using the new function
ofSendMessage(string).

When you call ofSendMessage, the function
testApp::gotMessage(ofMessage msg) is called by the
event system.

You can use this as an easy way to notify
your application about certain simple events.

Works everywhere!
Simple messaging


 void testApp::setup(){
 
 ofSendMessage("ready");
 }

 void testApp::gotMessage(ofMessage msg){
 
 cout << "got message: " << msg.message << endl;
 }
Register for messages
When you create your own class and you want to
receive messages, you create a function called void
gotMessage(ofMessage& msg) and register using
ofRegisterGetMessages(this)

              class MyMessageReciever {
              public:
              
 MyMessageReciever() {
              
 }
              
              
 void setup() {
              
 
     ofRegisterGetMessages(this);
              
              
 }
              
 void gotMessage(ofMessage& msg) {
              
 
     cout << "got message: " << msg.message << endl;
              
 }
              }
Common events
Mouse events      Touch events

• mouseMoved      • touchDown
• mouseDragged    • touchUp
• mousePressed    • touchMoved
• mouseReleased   • touchDoubleTap
                  • touchCancelled
Key events        Audio events

• keyPressed      • audioReceived
• keyReleased     • audioRequested
Common events

Application events   Simple messages

• setup              • messageEvent
• update
• draw               File drag events

• exit               • fileDragEvent
• windowResized
Event argument objects
ofKeyEventArgs     ofAudioEventArgs
int key            float* buffer
                   int bufferSize
ofMouseEventArgs   int nChannels
int x
int y              ofResizeEventArgs
int button         int width
                   int height
Event argument objects
 ofTouchEventArgs
 int id             float minoraxis
 int time           float majoraxis
 float x             float pressure
 float y             float xspeed
 int numTouches     float yspeed
 float width         float xaccel
 float height        float yaccel
 float angle
Custom events

In your header file (.h) create an extern event object
which will be used as a storage for listeners and
which notifies them.

“extern” tells your compiler the object is declared
somewhere else. This will be done in your .cpp file.
Also, define your custom event data type.
Custom events
Step 1. create your custom event data type


class MyCustomEventData {
public:

 MyCustomEventData(string someData):data(someData) {

 }

 string data;
};


Step 2. create a extern declared event dispatcher object.

extern ofEvent<MyCustomEventData> myCustomEventDispatcher;

                                                            The event dispatcher
  The data type you will pass to listener
Custom events
Step 3. in your .cpp define the event dispatcher object.
ofEvent<MyCustomEventData> myCustomEventDispatcher;




To listen to this myCustomerEventDispatcher you
create a event listener function and call
ofAddListener like this:
ofAddListener(myCustomEventDispatcher, this, &testApp::myCustomEventListener);




This is how the event listener function looks.

class testApp : public ofBaseApp{

 public:

 
      void myCustomEventListener(MyCustomEventData& args) {

 
      
   cout << "Received a custom event: " << args.data << endl;

 
      }
}
roxlu
www.roxlu.com

More Related Content

What's hot

メディア・アートII 第2回 openFrameworks基礎 配列、くりかえし、乱数 ベクトルを使用したアニメーション
メディア・アートII  第2回 openFrameworks基礎 配列、くりかえし、乱数 ベクトルを使用したアニメーションメディア・アートII  第2回 openFrameworks基礎 配列、くりかえし、乱数 ベクトルを使用したアニメーション
メディア・アートII 第2回 openFrameworks基礎 配列、くりかえし、乱数 ベクトルを使用したアニメーションAtsushi Tadokoro
 
Unityで PhotonCloudを使ってリアルタイム・マルチプレイヤーゲームを作っちゃおう【導入編】
Unityで PhotonCloudを使ってリアルタイム・マルチプレイヤーゲームを作っちゃおう【導入編】Unityで PhotonCloudを使ってリアルタイム・マルチプレイヤーゲームを作っちゃおう【導入編】
Unityで PhotonCloudを使ってリアルタイム・マルチプレイヤーゲームを作っちゃおう【導入編】GMO GlobalSign Holdings K.K.
 
Squirrel
SquirrelSquirrel
Squirrelmelpon
 
OPcache の最適化器の今
OPcache の最適化器の今OPcache の最適化器の今
OPcache の最適化器の今y-uti
 
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップUnite2017Tokyo
 
Forge UI: A New Way to Customize the Atlassian User Experience
Forge UI: A New Way to Customize the Atlassian User ExperienceForge UI: A New Way to Customize the Atlassian User Experience
Forge UI: A New Way to Customize the Atlassian User ExperienceAtlassian
 
기가박스 영화관 운영 시스템 구축마지막
기가박스 영화관 운영 시스템 구축마지막기가박스 영화관 운영 시스템 구축마지막
기가박스 영화관 운영 시스템 구축마지막ssuser5280ce
 
php and sapi and zendengine2 and...
php and sapi and zendengine2 and...php and sapi and zendengine2 and...
php and sapi and zendengine2 and...do_aki
 
Unityで Photonを使ってリアルタイム・マルチプレイヤーゲームを作っちゃおう【応用編】
Unityで Photonを使ってリアルタイム・マルチプレイヤーゲームを作っちゃおう【応用編】Unityで Photonを使ってリアルタイム・マルチプレイヤーゲームを作っちゃおう【応用編】
Unityで Photonを使ってリアルタイム・マルチプレイヤーゲームを作っちゃおう【応用編】GMO GlobalSign Holdings K.K.
 
unique_ptrにポインタ以外のものを持たせるとき
unique_ptrにポインタ以外のものを持たせるときunique_ptrにポインタ以外のものを持たせるとき
unique_ptrにポインタ以外のものを持たせるときShintarou Okada
 
BlueHat v17 || Out of the Truman Show: VM Escape in VMware Gracefully
BlueHat v17 || Out of the Truman Show: VM Escape in VMware Gracefully BlueHat v17 || Out of the Truman Show: VM Escape in VMware Gracefully
BlueHat v17 || Out of the Truman Show: VM Escape in VMware Gracefully BlueHat Security Conference
 
【Unite 2018 Tokyo】そろそろ楽がしたい!新アセットバンドルワークフロー&リソースマネージャー詳細解説
【Unite 2018 Tokyo】そろそろ楽がしたい!新アセットバンドルワークフロー&リソースマネージャー詳細解説【Unite 2018 Tokyo】そろそろ楽がしたい!新アセットバンドルワークフロー&リソースマネージャー詳細解説
【Unite 2018 Tokyo】そろそろ楽がしたい!新アセットバンドルワークフロー&リソースマネージャー詳細解説Unity Technologies Japan K.K.
 
02.실전! 시스템 관리자를 위한 Ansible
02.실전! 시스템 관리자를 위한 Ansible02.실전! 시스템 관리자를 위한 Ansible
02.실전! 시스템 관리자를 위한 AnsibleOpennaru, inc.
 
hbstudy20100821 SpamAssassin
hbstudy20100821 SpamAssassinhbstudy20100821 SpamAssassin
hbstudy20100821 SpamAssassinTakashi Takizawa
 
『ラブライブ!スクールアイドルフェスティバル ALL STARS』における開発事例 ~システムUIの管理についてご紹介~
『ラブライブ!スクールアイドルフェスティバル ALL STARS』における開発事例 ~システムUIの管理についてご紹介~『ラブライブ!スクールアイドルフェスティバル ALL STARS』における開発事例 ~システムUIの管理についてご紹介~
『ラブライブ!スクールアイドルフェスティバル ALL STARS』における開発事例 ~システムUIの管理についてご紹介~KLab Inc. / Tech
 
OPcacheの新機能ファイルベースキャッシュの内部実装を読んでみた
OPcacheの新機能ファイルベースキャッシュの内部実装を読んでみたOPcacheの新機能ファイルベースキャッシュの内部実装を読んでみた
OPcacheの新機能ファイルベースキャッシュの内部実装を読んでみたYoshio Hanawa
 
CSW2017 Qinghao tang+Xinlei ying vmware_escape_final
CSW2017 Qinghao tang+Xinlei ying vmware_escape_finalCSW2017 Qinghao tang+Xinlei ying vmware_escape_final
CSW2017 Qinghao tang+Xinlei ying vmware_escape_finalCanSecWest
 
UniRxことはじめ
UniRxことはじめUniRxことはじめ
UniRxことはじめShoichi Yasui
 

What's hot (20)

メディア・アートII 第2回 openFrameworks基礎 配列、くりかえし、乱数 ベクトルを使用したアニメーション
メディア・アートII  第2回 openFrameworks基礎 配列、くりかえし、乱数 ベクトルを使用したアニメーションメディア・アートII  第2回 openFrameworks基礎 配列、くりかえし、乱数 ベクトルを使用したアニメーション
メディア・アートII 第2回 openFrameworks基礎 配列、くりかえし、乱数 ベクトルを使用したアニメーション
 
Unityで PhotonCloudを使ってリアルタイム・マルチプレイヤーゲームを作っちゃおう【導入編】
Unityで PhotonCloudを使ってリアルタイム・マルチプレイヤーゲームを作っちゃおう【導入編】Unityで PhotonCloudを使ってリアルタイム・マルチプレイヤーゲームを作っちゃおう【導入編】
Unityで PhotonCloudを使ってリアルタイム・マルチプレイヤーゲームを作っちゃおう【導入編】
 
Squirrel
SquirrelSquirrel
Squirrel
 
OPcache の最適化器の今
OPcache の最適化器の今OPcache の最適化器の今
OPcache の最適化器の今
 
Hair Groom入門 (UE4 Character Art Dive Online)
Hair Groom入門 (UE4 Character Art Dive Online)Hair Groom入門 (UE4 Character Art Dive Online)
Hair Groom入門 (UE4 Character Art Dive Online)
 
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
 
Forge UI: A New Way to Customize the Atlassian User Experience
Forge UI: A New Way to Customize the Atlassian User ExperienceForge UI: A New Way to Customize the Atlassian User Experience
Forge UI: A New Way to Customize the Atlassian User Experience
 
기가박스 영화관 운영 시스템 구축마지막
기가박스 영화관 운영 시스템 구축마지막기가박스 영화관 운영 시스템 구축마지막
기가박스 영화관 운영 시스템 구축마지막
 
php and sapi and zendengine2 and...
php and sapi and zendengine2 and...php and sapi and zendengine2 and...
php and sapi and zendengine2 and...
 
Unityで Photonを使ってリアルタイム・マルチプレイヤーゲームを作っちゃおう【応用編】
Unityで Photonを使ってリアルタイム・マルチプレイヤーゲームを作っちゃおう【応用編】Unityで Photonを使ってリアルタイム・マルチプレイヤーゲームを作っちゃおう【応用編】
Unityで Photonを使ってリアルタイム・マルチプレイヤーゲームを作っちゃおう【応用編】
 
unique_ptrにポインタ以外のものを持たせるとき
unique_ptrにポインタ以外のものを持たせるときunique_ptrにポインタ以外のものを持たせるとき
unique_ptrにポインタ以外のものを持たせるとき
 
BlueHat v17 || Out of the Truman Show: VM Escape in VMware Gracefully
BlueHat v17 || Out of the Truman Show: VM Escape in VMware Gracefully BlueHat v17 || Out of the Truman Show: VM Escape in VMware Gracefully
BlueHat v17 || Out of the Truman Show: VM Escape in VMware Gracefully
 
Geometry with Unity
Geometry with UnityGeometry with Unity
Geometry with Unity
 
【Unite 2018 Tokyo】そろそろ楽がしたい!新アセットバンドルワークフロー&リソースマネージャー詳細解説
【Unite 2018 Tokyo】そろそろ楽がしたい!新アセットバンドルワークフロー&リソースマネージャー詳細解説【Unite 2018 Tokyo】そろそろ楽がしたい!新アセットバンドルワークフロー&リソースマネージャー詳細解説
【Unite 2018 Tokyo】そろそろ楽がしたい!新アセットバンドルワークフロー&リソースマネージャー詳細解説
 
02.실전! 시스템 관리자를 위한 Ansible
02.실전! 시스템 관리자를 위한 Ansible02.실전! 시스템 관리자를 위한 Ansible
02.실전! 시스템 관리자를 위한 Ansible
 
hbstudy20100821 SpamAssassin
hbstudy20100821 SpamAssassinhbstudy20100821 SpamAssassin
hbstudy20100821 SpamAssassin
 
『ラブライブ!スクールアイドルフェスティバル ALL STARS』における開発事例 ~システムUIの管理についてご紹介~
『ラブライブ!スクールアイドルフェスティバル ALL STARS』における開発事例 ~システムUIの管理についてご紹介~『ラブライブ!スクールアイドルフェスティバル ALL STARS』における開発事例 ~システムUIの管理についてご紹介~
『ラブライブ!スクールアイドルフェスティバル ALL STARS』における開発事例 ~システムUIの管理についてご紹介~
 
OPcacheの新機能ファイルベースキャッシュの内部実装を読んでみた
OPcacheの新機能ファイルベースキャッシュの内部実装を読んでみたOPcacheの新機能ファイルベースキャッシュの内部実装を読んでみた
OPcacheの新機能ファイルベースキャッシュの内部実装を読んでみた
 
CSW2017 Qinghao tang+Xinlei ying vmware_escape_final
CSW2017 Qinghao tang+Xinlei ying vmware_escape_finalCSW2017 Qinghao tang+Xinlei ying vmware_escape_final
CSW2017 Qinghao tang+Xinlei ying vmware_escape_final
 
UniRxことはじめ
UniRxことはじめUniRxことはじめ
UniRxことはじめ
 

Viewers also liked

openFrameworks 007 - video
openFrameworks 007 - videoopenFrameworks 007 - video
openFrameworks 007 - videoroxlu
 
openFrameworks 007 - utils
openFrameworks 007 - utilsopenFrameworks 007 - utils
openFrameworks 007 - utilsroxlu
 
openFrameworks 007 - graphics
openFrameworks 007 - graphicsopenFrameworks 007 - graphics
openFrameworks 007 - graphicsroxlu
 
openFrameworks 007 - GL
openFrameworks 007 - GL openFrameworks 007 - GL
openFrameworks 007 - GL roxlu
 
openFrameworks 007 - 3D
openFrameworks 007 - 3DopenFrameworks 007 - 3D
openFrameworks 007 - 3Droxlu
 
Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013aleks-f
 
Computer vision techniques for interactive art
Computer vision techniques for interactive artComputer vision techniques for interactive art
Computer vision techniques for interactive artJorge Cardoso
 
Open frameworks 101_fitc
Open frameworks 101_fitcOpen frameworks 101_fitc
Open frameworks 101_fitcbenDesigning
 
openFrameworks 007 - sound
openFrameworks 007 - soundopenFrameworks 007 - sound
openFrameworks 007 - soundroxlu
 
Media Art II 2013 第5回:openFrameworks Addonを使用する
Media Art II 2013 第5回:openFrameworks Addonを使用するMedia Art II 2013 第5回:openFrameworks Addonを使用する
Media Art II 2013 第5回:openFrameworks Addonを使用するAtsushi Tadokoro
 

Viewers also liked (10)

openFrameworks 007 - video
openFrameworks 007 - videoopenFrameworks 007 - video
openFrameworks 007 - video
 
openFrameworks 007 - utils
openFrameworks 007 - utilsopenFrameworks 007 - utils
openFrameworks 007 - utils
 
openFrameworks 007 - graphics
openFrameworks 007 - graphicsopenFrameworks 007 - graphics
openFrameworks 007 - graphics
 
openFrameworks 007 - GL
openFrameworks 007 - GL openFrameworks 007 - GL
openFrameworks 007 - GL
 
openFrameworks 007 - 3D
openFrameworks 007 - 3DopenFrameworks 007 - 3D
openFrameworks 007 - 3D
 
Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013
 
Computer vision techniques for interactive art
Computer vision techniques for interactive artComputer vision techniques for interactive art
Computer vision techniques for interactive art
 
Open frameworks 101_fitc
Open frameworks 101_fitcOpen frameworks 101_fitc
Open frameworks 101_fitc
 
openFrameworks 007 - sound
openFrameworks 007 - soundopenFrameworks 007 - sound
openFrameworks 007 - sound
 
Media Art II 2013 第5回:openFrameworks Addonを使用する
Media Art II 2013 第5回:openFrameworks Addonを使用するMedia Art II 2013 第5回:openFrameworks Addonを使用する
Media Art II 2013 第5回:openFrameworks Addonを使用する
 

Similar to openFrameworks 007 - events

Event handling
Event handlingEvent handling
Event handlingswapnac12
 
Java gui event
Java gui eventJava gui event
Java gui eventSoftNutx
 
tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handlingteach4uin
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5sotlsoc
 
Unit-3 event handling
Unit-3 event handlingUnit-3 event handling
Unit-3 event handlingAmol Gaikwad
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03Ankit Dubey
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptxusvirat1805
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptxGood657694
 
EventBus for Android
EventBus for AndroidEventBus for Android
EventBus for Androidgreenrobot
 
Dr Jammi Ashok - Introduction to Java Material (OOPs)
 Dr Jammi Ashok - Introduction to Java Material (OOPs) Dr Jammi Ashok - Introduction to Java Material (OOPs)
Dr Jammi Ashok - Introduction to Java Material (OOPs)jammiashok123
 
File Handling
File HandlingFile Handling
File HandlingSohanur63
 
Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2PRN USM
 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVASrajan Shukla
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events WebStackAcademy
 
java Unit4 chapter1 applets
java Unit4 chapter1 appletsjava Unit4 chapter1 applets
java Unit4 chapter1 appletsraksharao
 

Similar to openFrameworks 007 - events (20)

Event handling
Event handlingEvent handling
Event handling
 
Event handling
Event handlingEvent handling
Event handling
 
Java gui event
Java gui eventJava gui event
Java gui event
 
tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handling
 
What is Event
What is EventWhat is Event
What is Event
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5
 
Unit-3 event handling
Unit-3 event handlingUnit-3 event handling
Unit-3 event handling
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
 
Java-Events
Java-EventsJava-Events
Java-Events
 
Events1
Events1Events1
Events1
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
 
EventBus for Android
EventBus for AndroidEventBus for Android
EventBus for Android
 
Dr Jammi Ashok - Introduction to Java Material (OOPs)
 Dr Jammi Ashok - Introduction to Java Material (OOPs) Dr Jammi Ashok - Introduction to Java Material (OOPs)
Dr Jammi Ashok - Introduction to Java Material (OOPs)
 
File Handling
File HandlingFile Handling
File Handling
 
Androd Listeners
Androd ListenersAndrod Listeners
Androd Listeners
 
Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2
 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVA
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
 
java Unit4 chapter1 applets
java Unit4 chapter1 appletsjava Unit4 chapter1 applets
java Unit4 chapter1 applets
 

Recently uploaded

Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 

Recently uploaded (20)

Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 

openFrameworks 007 - events

  • 1. openFrameworks events
  • 2. Events • Send notifications on certain events or create your own. • Wraps around Poco Events system • You register for the events you want to receive. • testApp receives events automatically ( , keyPressed keyReleased, mouseMoved)
  • 3. Events Delegates • A target subscribes to an event by registering a delegate. • You can see the delegate as an “Event listener” • An event has always one argument. • There is one global ofEvents object which holds the OF related, event instances to which you can subscribe. • OF uses a First In First Out (FIFO) event queue.
  • 4. Events Listen for OF-events Create a function which is called when the event notification is sent. This function must have one argument of the correct event type. Use ofAddListener(event object, callback object, callback function) to tell the event system you want to listen for events. You can remove your regiseterd listener by calling ofRemoveListener(event object, callback object, callback function)
  • 5. Register to an event 1. Decide to what event you want to listen See events/ofEvents.h for the available OF events in the ofCoreEvent class. For example: ofEvent<ofMouseEventArgs> mouseMoved The name of the event is mouseMoved and passes am ofMouseEventArgs to the callback.
  • 6. Register to an event Name of event is class ofCoreEvents { public: mouseMoved which ofEvent<ofEventArgs> ofEvent<ofEventArgs> setup; update; stores the listeners ofEvent<ofEventArgs> draw; ofEvent<ofEventArgs> exit; ofEvent<ofResizeEventArgs> indowResized; w ofEvent<ofKeyEventArgs> keyPressed; ofEvent<ofKeyEventArgs> keyReleased; ofEvent<ofMouseEventArgs> mouseMoved; ofEvent<ofMouseEventArgs> mouseDragged; ofEvent<ofMouseEventArgs> mousePressed; ofMouseEventArgs is ofEvent<ofMouseEventArgs> mouseReleased; ... passed as parameter to } the callback
  • 7. Register to an event 2. Create the callback function Create a function which receives the event type as first and only argument. ofEvent<ofMouseEventArgs> mouseMoved class testApp : public ofBaseApp{ public: void myCustomMouseReleased(ofMouseEventArgs& args) { cout << "received a mouse released event" << endl; cout << "mouse x:" << args.x << endl; cout << "mouse y:" << args.y << endl; cout << "mouse button:" << args.button << endl; } }
  • 8. Register to an event 3. Tell the event object you want to listen Use the global function ofAddListener to register your new function as a callback for the event. Note that ofEvents is the global instance of ofCoreEvents void testApp::setup(){ ofAddListener( ofEvents.mouseReleased // the event object ,this // testApp pointer ,&testApp::myCustomMouseReleased // the callback function. ); }
  • 9. UnRegister from event To unregister from an event, you simply call ofRemoveListener() with the same parameters when you added the listener. You can re-enable the listener again by calling ofAddListener() again. void testApp::setup(){ ofRemoveListener( ofEvents.mouseReleased ,this ,&testApp::myCustomMouseReleased ); }
  • 10. Recap event listening Listen for OF-events 1. Create listener function 2. Find the event you want to listen to. 3. Call ofAddListener() to register Remove listener 1. Call ofRemoveListener()
  • 11. Event listening Register to all mouse events Create these functions: void mouseDragged(ofMouseEventArgs& args) void mouseMoved(ofMouseEventArgs& args) void mousePressed(ofMouseEventArgs& args) void mouseReleased(ofMouseEventArgs& args) Call ofRegisterMouseEvents(this) to register to all mouse events. Instead of registering for each one separately. To unregister call ofUnregisterMouseEvents(this)
  • 12. Event listening class MyWorker { public: MyWorker() { } void setup() { ofRegisterMouseEvents(this); } void mouseMoved(ofMouseEventArgs& args) { cout << "Moved: " << args.x << ", " << args.y << endl; } void mouseDragged(ofMouseEventArgs& args) { } void mousePressed(ofMouseEventArgs& args) { } void mouseReleased(ofMouseEventArgs& args) { } };
  • 13. Event listening Register to all keyboard events Create these functions: void keyPressed(ofKeyEventArgs& args) void keyReleased(ofKeyEventArgs& args) Call ofRegisterKeyEvents(this) to register to all key events. To unregister call ofUnregisterKeyEvents(this)
  • 14. Event listening Register to all touch events Create these functions: void touchDoubleTap(ofTouchEventArgs& args) void touchDown(ofTouchEventArgs& args) void touchMoved(ofTouchEventArgs& args) void touchUp(ofTouchEventArgs& args) void touchCancelled(ofTouchEventArgs& args) Call ofRegisterTouchEvents(this) to register to all touch events. Unregister using ofUnregisterTouchEvents(this)
  • 15. Event listening Register to all drag events Create these functions: void dragEvent(ofDragInfo& args) Call ofRegisterDragEvents(this) to register to all drag events. Unregister with ofUnregisterDragEvents(this)
  • 16. Simple messaging openFrameworks adds a very simple way to send messages between objects using the new function ofSendMessage(string). When you call ofSendMessage, the function testApp::gotMessage(ofMessage msg) is called by the event system. You can use this as an easy way to notify your application about certain simple events. Works everywhere!
  • 17. Simple messaging void testApp::setup(){ ofSendMessage("ready"); } void testApp::gotMessage(ofMessage msg){ cout << "got message: " << msg.message << endl; }
  • 18. Register for messages When you create your own class and you want to receive messages, you create a function called void gotMessage(ofMessage& msg) and register using ofRegisterGetMessages(this) class MyMessageReciever { public: MyMessageReciever() { } void setup() { ofRegisterGetMessages(this); } void gotMessage(ofMessage& msg) { cout << "got message: " << msg.message << endl; } }
  • 19. Common events Mouse events Touch events • mouseMoved • touchDown • mouseDragged • touchUp • mousePressed • touchMoved • mouseReleased • touchDoubleTap • touchCancelled Key events Audio events • keyPressed • audioReceived • keyReleased • audioRequested
  • 20. Common events Application events Simple messages • setup • messageEvent • update • draw File drag events • exit • fileDragEvent • windowResized
  • 21. Event argument objects ofKeyEventArgs ofAudioEventArgs int key float* buffer int bufferSize ofMouseEventArgs int nChannels int x int y ofResizeEventArgs int button int width int height
  • 22. Event argument objects ofTouchEventArgs int id float minoraxis int time float majoraxis float x float pressure float y float xspeed int numTouches float yspeed float width float xaccel float height float yaccel float angle
  • 23. Custom events In your header file (.h) create an extern event object which will be used as a storage for listeners and which notifies them. “extern” tells your compiler the object is declared somewhere else. This will be done in your .cpp file. Also, define your custom event data type.
  • 24. Custom events Step 1. create your custom event data type class MyCustomEventData { public: MyCustomEventData(string someData):data(someData) { } string data; }; Step 2. create a extern declared event dispatcher object. extern ofEvent<MyCustomEventData> myCustomEventDispatcher; The event dispatcher The data type you will pass to listener
  • 25. Custom events Step 3. in your .cpp define the event dispatcher object. ofEvent<MyCustomEventData> myCustomEventDispatcher; To listen to this myCustomerEventDispatcher you create a event listener function and call ofAddListener like this: ofAddListener(myCustomEventDispatcher, this, &testApp::myCustomEventListener); This is how the event listener function looks. class testApp : public ofBaseApp{ public: void myCustomEventListener(MyCustomEventData& args) { cout << "Received a custom event: " << args.data << endl; } }

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n