Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

24
LINES

< > BotCompany Repo | #1034716 // tempAddGlobalCtrlKeyListener - add a global AWT listener trying to detect whether the ctrl key is currently pressed

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (5081L/28K).

// This works when the ctrl key is pressed/released when a Swing window
// is in focus or the mouse is inside a Swing window.
// If you press ctrl before the program starts and the mouse is never
// inside any of the windows, the key press will not be registered.

static AutoCloseable tempAddGlobalCtrlKeyListener(IVF1<Bool> onStatusChanged) {
  AWTEventListener l = new AWTEventListener {
    Bool status;
    
    public void eventDispatched(AWTEvent evt) {
      if (evt cast InputEvent) {
        Bool b = evt.isControlDown();
        if (!eq(b, status))
          pcallF_typed(onStatusChanged, status = b);
      }
    }
  };
  
  Toolkit.getDefaultToolkit().addAWTEventListener(l,
    AWTEvent.KEY_EVENT_MASK |
    AWTEvent.MOUSE_EVENT_MASK |
    AWTEvent.MOUSE_MOTION_EVENT_MASK);
  ret -> Toolkit.getDefaultToolkit().removeAWTEventListener(l);
}

Author comment

Began life as a copy of #1020885

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034716
Snippet name: tempAddGlobalCtrlKeyListener - add a global AWT listener trying to detect whether the ctrl key is currently pressed
Eternal ID of this version: #1034716/4
Text MD5: 59236176ea5d42fc583a925e399eaa52
Transpilation MD5: a92c438ecb587cd2b995d0b568f77fca
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-07 14:35:01
Source code size: 877 bytes / 24 lines
Pitched / IR pitched: No / No
Views / Downloads: 75 / 123
Version history: 3 change(s)
Referenced in: [show references]