Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Hotkey program  (Read 4416 times)

0 Members and 1 Guest are viewing this topic.

Tan_Za

    Topic Starter


    Intermediate
  • Starcraft and C programming
    • Experience: Experienced
    • OS: Windows 7
    Hotkey program
    « on: April 03, 2012, 08:42:18 PM »
    I want to make a program in C that will capture a keystroke. By this i mean a global keystroke such as when i press F9 my program will leave the system tray and show up on the screen regardless of the window i am currently on. I could be browsing the internet, then i wish to bring up my program so i press F9 and it shows up. Does this make sense?
    For windows 7 operating system.

    So ill just clarify this, I want my program to leave the system tray and display when i press the F9 key. Program can be a windowed program or console, does not matter at this point. At the current time my program in console but i can change it later or if the need arises to do so.

    Thanks for help,
    Tan_Za

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: Hotkey program
    « Reply #1 on: April 03, 2012, 09:36:59 PM »
    http://msdn.microsoft.com/en-us/library/windows/desktop/ms644960%28v=vs.85%29.aspx

    Two points:

    In order to be system-wide, the hook procedure must be in a DLL, otherwise it will only trap events in a single processes input queue.

    WH_KEYBOARD is the hook type you would need, or possibly WH_KEYBOARD_LL.
    I was trying to dereference Null Pointers before it was cool.

    Tan_Za

      Topic Starter


      Intermediate
    • Starcraft and C programming
      • Experience: Experienced
      • OS: Windows 7
      Re: Hotkey program
      « Reply #2 on: April 16, 2012, 10:36:06 PM »
      what about c++? do the same conventions apply?

      thanks for response btw :)
      tan_za

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: Hotkey program
      « Reply #3 on: April 16, 2012, 10:53:26 PM »
      what about c++? do the same conventions apply?
      Yes. I don't know of any C++ libraries that handle hooking for you, but I imagine there are some you could use to make it easier; but it all boils down to calls to those functions
      I was trying to dereference Null Pointers before it was cool.

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: Hotkey program
      « Reply #4 on: April 16, 2012, 11:32:05 PM »
      AutoHotkeyr has source code available.
      Anyone not familiar with it may wish to loo this over:
      http://en.wikipedia.org/wiki/AutoHotkey
      Using F9 would be a good choice if it is not needed by other programs.
      It is not in this list.
      http://www.computerhope.com/shortcut/windows.htm

      Linux711



        Mentor

        Thanked: 59
        • Yes
        • Programming Blog
      • Certifications: List
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 7
      Re: Hotkey program
      « Reply #5 on: April 22, 2012, 07:53:22 PM »
      Look up GetAsyncKeyState. It should be on msdn. That can detect keystrokes even when not in focus.
      YouTube

      "Genius is persistence, not brain power." - Me

      "Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

      Linux711



        Mentor

        Thanked: 59
        • Yes
        • Programming Blog
      • Certifications: List
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 7
      Re: Hotkey program
      « Reply #6 on: April 22, 2012, 08:01:18 PM »
      Here is an example using F8. It will work without being in focus.

      Code: [Select]
      #include <stdio.h>
      #include <windows.h>

      int main()
      {

          while(1){ //infinite loop!
                   if(GetAsyncKeyState(VK_F8))
                   {
                      printf("You pressed F8\n");
                   }

                   Sleep(100); // loop will only start again after 1/10 of a second
          }
          return 0;
      }
      YouTube

      "Genius is persistence, not brain power." - Me

      "Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

      Tan_Za

        Topic Starter


        Intermediate
      • Starcraft and C programming
        • Experience: Experienced
        • OS: Windows 7
        Re: Hotkey program
        « Reply #7 on: May 08, 2012, 07:50:28 PM »
        Thanks for your responses guys all really helpful!! :)
        I do like that last solution, it works really well.

        Thanks for the help guys and all the solutions are very interesting and work quite well,
        Tan_Za

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: Hotkey program
        « Reply #8 on: May 08, 2012, 08:03:03 PM »
        When I was writing a Menu "commandbar" some years ago for Visual Basic 6 (the details aren't important, but it basically rehosted the menu on a toolbar control, similar to IE4). One of the troubles was that pressing left and right wouldn't always work properly since the toolbar didn't have menu-like keyboard navigation. Anyway, the end result was that I needed to deal with those keys myself. I believe my original attempts used GetAsyncKeyState() but it proved unreliable, which was why I resorted to using a Windows Hook. I don't remember the specifics of what "unreliable" was but it was enough to force me to look for other alternatives.

        With the GetAsyncKeyState method you would probably need to make sure to set a flag, otherwise you'll fire the "hotkey" event every single iteration when you check it; additionally, you're somewhat limited by the rate at which it iterates. (it should work however, since GetAsyncKeyState returns whether the key is currently pressed as well as if it was pressed since the last call).


        My own problem with this approach is it is polling constantly; whether that matters I suppose depends on what you want to use it for.
        I was trying to dereference Null Pointers before it was cool.