Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Global key press [SOLVED]

Global key press [SOLVED]

Scheduled Pinned Locked Moved C / C++ / MFC
question
11 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Valentinor
    wrote on last edited by
    #1

    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.

    J 1 Reply Last reply
    0
    • V Valentinor

      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.

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      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.

      V 2 Replies Last reply
      0
      • J Jochen Arndt

        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.

        V Offline
        V Offline
        Valentinor
        wrote on last edited by
        #3

        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.

        J L 2 Replies Last reply
        0
        • V Valentinor

          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.

          J Offline
          J Offline
          Jochen Arndt
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • V Valentinor

            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.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • J Jochen Arndt

              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.

              V Offline
              V Offline
              Valentinor
              wrote on last edited by
              #6

              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!

              J 1 Reply Last reply
              0
              • V Valentinor

                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!

                J Offline
                J Offline
                Jochen Arndt
                wrote on last edited by
                #7

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

                V 2 Replies Last reply
                0
                • J Jochen Arndt

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

                  V Offline
                  V Offline
                  Valentinor
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • J Jochen Arndt

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

                    V Offline
                    V Offline
                    Valentinor
                    wrote on last edited by
                    #9

                    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.

                    J 1 Reply Last reply
                    0
                    • V Valentinor

                      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.

                      J Offline
                      J Offline
                      Jochen Arndt
                      wrote on last edited by
                      #10

                      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)[^].

                      V 1 Reply Last reply
                      0
                      • J Jochen Arndt

                        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)[^].

                        V Offline
                        V Offline
                        Valentinor
                        wrote on last edited by
                        #11

                        Ok, thanks!

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups