Global key press [SOLVED]
-
Hi, What is the recommended way to listen for a key press globally? At first, adding an infinite loop came to mind (for(;;), or while(true)), but these are bad choices, as they eat a lot of CPU, and adding sleep will cause to lose some of the key press events. I want to make it to check any key (or key combination), to see what is pressed on Windows OS.
-
Hi, What is the recommended way to listen for a key press globally? At first, adding an infinite loop came to mind (for(;;), or while(true)), but these are bad choices, as they eat a lot of CPU, and adding sleep will cause to lose some of the key press events. I want to make it to check any key (or key combination), to see what is pressed on Windows OS.
It depends if "globally" applies to the system (user session) or an application. For an application it depends on the type (console or GUI). With GUI applications it depends also if you want to catch all / multiple or only a single / few key combinations. The most "global" method is hooking keyboard events with SetWindowsHookEx function (Windows)[^]. For application level handling see About Keyboard Input | Microsoft Docs[^] and follow the links. To handle only a few specific keyboard events for GUI applications use Accelerators (Hotkeys) processed by the main window of the application.
-
It depends if "globally" applies to the system (user session) or an application. For an application it depends on the type (console or GUI). With GUI applications it depends also if you want to catch all / multiple or only a single / few key combinations. The most "global" method is hooking keyboard events with SetWindowsHookEx function (Windows)[^]. For application level handling see About Keyboard Input | Microsoft Docs[^] and follow the links. To handle only a few specific keyboard events for GUI applications use Accelerators (Hotkeys) processed by the main window of the application.
This function will be called from Java, which app will be minimized, or maybe running in the background. The app won't be focused, so yes, I need it for the entire user session, even if another application is focused, I want it to still register the key press and do something in case a key I randomly choose is pressed. I will make some profiles: profile 1 has key "A", profile 2 has key "CTRL + A", those combinations will be changeable.
-
This function will be called from Java, which app will be minimized, or maybe running in the background. The app won't be focused, so yes, I need it for the entire user session, even if another application is focused, I want it to still register the key press and do something in case a key I randomly choose is pressed. I will make some profiles: profile 1 has key "A", profile 2 has key "CTRL + A", those combinations will be changeable.
You might have a look at the AutoIt Scripting Language. I guess you can use that to do what you want. In any case you should not use a key combination that is used by Windows itself or common applications. See Keyboard shortcuts in Windows for a list of combinations that should not be redirected.
-
This function will be called from Java, which app will be minimized, or maybe running in the background. The app won't be focused, so yes, I need it for the entire user session, even if another application is focused, I want it to still register the key press and do something in case a key I randomly choose is pressed. I will make some profiles: profile 1 has key "A", profile 2 has key "CTRL + A", those combinations will be changeable.
Valentinor wrote:
I want it to still register the key press and do something in case a key I randomly choose is pressed. I will make some profiles: profile 1 has key "A", profile 2 has key "CTRL + A",
Sounds like you are looking for the RegisterHotKey function[^]. Best Wishes, -David Delaune
-
It depends if "globally" applies to the system (user session) or an application. For an application it depends on the type (console or GUI). With GUI applications it depends also if you want to catch all / multiple or only a single / few key combinations. The most "global" method is hooking keyboard events with SetWindowsHookEx function (Windows)[^]. For application level handling see About Keyboard Input | Microsoft Docs[^] and follow the links. To handle only a few specific keyboard events for GUI applications use Accelerators (Hotkeys) processed by the main window of the application.
For what I have now, I'll use only one key, and I used "SetWindowsHookEx". Is it possible to use that for a combination of keys? I don't need it in this application, but in future I will need it. All the examples I saw on google were with only 1 key, or using the mouse. If you can use it for key combination, can it be used to combine 4-5 keys? Yes, it is hard to press that many at a time, but you can map an extra key from the keyboard to press all of them at the same time. Thanks for you help!
-
For what I have now, I'll use only one key, and I used "SetWindowsHookEx". Is it possible to use that for a combination of keys? I don't need it in this application, but in future I will need it. All the examples I saw on google were with only 1 key, or using the mouse. If you can use it for key combination, can it be used to combine 4-5 keys? Yes, it is hard to press that many at a time, but you can map an extra key from the keyboard to press all of them at the same time. Thanks for you help!
With key combinations I meant a combination of a single key while also Shift, Ctrl, and or Alt are down. A combination of multiple keystrokes makes no sense because that can't be "eaten" (the initial strokes has to be passed and will be processed by the window having the focus). It is still unclear what you finally want to do. If is not a personal project, be very careful what you are doing. Such global "features" might interfere with the active application which is a "don't do it".
-
With key combinations I meant a combination of a single key while also Shift, Ctrl, and or Alt are down. A combination of multiple keystrokes makes no sense because that can't be "eaten" (the initial strokes has to be passed and will be processed by the window having the focus). It is still unclear what you finally want to do. If is not a personal project, be very careful what you are doing. Such global "features" might interfere with the active application which is a "don't do it".
Yes, sorry, I wasn't clear, by multiple combinations I was referring to Ctrl + Alt + Shift + AnotherKey (or other combinations). And yes, right now it kinda is a personal project. I'm using it to learn more, and I'll use it sometimes too when it is finished.
-
With key combinations I meant a combination of a single key while also Shift, Ctrl, and or Alt are down. A combination of multiple keystrokes makes no sense because that can't be "eaten" (the initial strokes has to be passed and will be processed by the window having the focus). It is still unclear what you finally want to do. If is not a personal project, be very careful what you are doing. Such global "features" might interfere with the active application which is a "don't do it".
So, let me reformulate my last statement. What is the alternative of SetWindowsHookEx, for a combination of keys, by checking if Ctrl and/or Alt and/or Shift is down + any other key (different from Windows combinations)? For a single button (keyboard or mouse) it is great, but from what I've seen, it can't be used to check at the same time if any Ctrl, Alt, Shift is down.
-
So, let me reformulate my last statement. What is the alternative of SetWindowsHookEx, for a combination of keys, by checking if Ctrl and/or Alt and/or Shift is down + any other key (different from Windows combinations)? For a single button (keyboard or mouse) it is great, but from what I've seen, it can't be used to check at the same time if any Ctrl, Alt, Shift is down.
The status of the Alt key is passed to the hook callback. The status of the Shift and Ctrl keys (and any other key) can be determined with the GetKeyState function (Windows)[^].
-
The status of the Alt key is passed to the hook callback. The status of the Shift and Ctrl keys (and any other key) can be determined with the GetKeyState function (Windows)[^].
Ok, thanks!