In a library how can I consume KeyEvent?
in
Library and Tool Development
•
3 months ago
I am making small library which can displays small menu over the running sketch. The menu is triggerred by release of the key INSERT. And whilst menu is displayed to exit from the menu, user has to press DELETE. (I just took for simplicity 2 keys which are not ASCII and rarely used in sketches).
The problem is that whilst menu is on screen, I want it to "consume" all the key events and don't let them propagate into the sketch. Is that something possible?
Please find below some skeleton for the library code:
The library registering itself with methods
- parent.registerMethod("draw", this);
- parent.registerMethod("keyEvent", this);
Here's some pseudocode from draw()
- public void draw(){
- if ( mIsMenuDisplayedFlag ){
- menu.draw();
- }
- else{
- // do nothing, sketch will run just as usual
- }
- }
And here's some pseudocode from keyEvent()
- /**
- * If menu is not displayed, we should wait for "INSERT" key to be released and then we change
- * isMenuDisplayedFlag AND CONSUME the event (so that sketch is not receiving it).
- *
- * If menu is displayed: we should consume all events (when menu is open: no keys should be received by
- * sketch) until "DELETE" key is to be released (Becuase ESC key seems to be already used to close the
- * sketch window so I don't want confusion and decided my menu to be closed with DELETE)
- */
- public void keyEvent(KeyEvent evt){
- if (mIsMenuDisplayed ){
- // consume all keys until DELETE is released
- if ( isReleasedKey(VK_DELETE) ){
- mIsMenuDisplayed = false;
- }
- !!!! CONSUME KEY HERE!!! ? how?
- }
- else{
- // sketch is running as usual, waiting for INSERT to be released to
- // trigger menu
- if ( isReleasedKey(VK_INSERT) ){
- mIsMenuDisplayed = true;
- !!! CONSUME KEY HERE!!!!!
- }
- }
- }
1