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).

1  
// This works when the ctrl key is pressed/released when a Swing window
2  
// is in focus or the mouse is inside a Swing window.
3  
// If you press ctrl before the program starts and the mouse is never
4  
// inside any of the windows, the key press will not be registered.
5  
6  
static AutoCloseable tempAddGlobalCtrlKeyListener(IVF1<Bool> onStatusChanged) {
7  
  AWTEventListener l = new AWTEventListener {
8  
    Bool status;
9  
    
10  
    public void eventDispatched(AWTEvent evt) {
11  
      if (evt cast InputEvent) {
12  
        Bool b = evt.isControlDown();
13  
        if (!eq(b, status))
14  
          pcallF_typed(onStatusChanged, status = b);
15  
      }
16  
    }
17  
  };
18  
  
19  
  Toolkit.getDefaultToolkit().addAWTEventListener(l,
20  
    AWTEvent.KEY_EVENT_MASK |
21  
    AWTEvent.MOUSE_EVENT_MASK |
22  
    AWTEvent.MOUSE_MOTION_EVENT_MASK);
23  
  ret -> Toolkit.getDefaultToolkit().removeAWTEventListener(l);
24  
}

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: 135 / 199
Version history: 3 change(s)
Referenced in: [show references]