Results 1 to 9 of 9

Thread: [2005] Global Hotkeys

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    50

    [2005] Global Hotkeys

    Hi,

    I want to have some global hotkeys, so when a certain key string is pressed (lets say Ctrl + X) and the main application is not in focus, so like another application or a game or movie is in focus, the main application will still recognize this keypress or hotkey and do some action.

    Can anyone help me with how do to this?

    Thanks,
    Jared.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Global Hotkeys

    Well, I wouldn't use Ctrl+X given that that is the universal shortcut for Cut, but what you need is a keyboard hook. There are lots of threads on the forum regarding this topic and lots of information on MSDN and the Web in general. I suggest that you search for terms like keyboard hook or keyboard hooking.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    50

    Re: [2005] Global Hotkeys

    Yeah, that was just an example, as I said .

    Thanks, I'll see what I can find and get back to ya.

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: [2005] Global Hotkeys

    I would go for the RegisterHotKey API...
    Code:
    Public Class Form1
    
        Private Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As IntPtr, ByVal id As Integer, _
                                                              ByVal fsModifiers As Integer, ByVal vk As Integer) _
                                                              As Integer
        Private Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As IntPtr, ByVal id As Integer) As Integer
    
        Private Const WM_HOTKEY As Integer = &H312
    
        Private Enum Modifiers
            MOD_ALT = &H1
            MOD_CONTROL = &H2
            MOD_SHIFT = &H4
            MOD_WIN = &H8
        End Enum
    
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    
            If m.Msg = WM_HOTKEY Then
                Me.Activate()
            End If
    
            MyBase.WndProc(m)
        End Sub
    
        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            UnregisterHotKey(Me.Handle, 101)
        End Sub
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            RegisterHotKey(Me.Handle, 101, Modifiers.MOD_CONTROL, Keys.K)
        End Sub
    
    End Class
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    50

    Re: [2005] Global Hotkeys

    Ah yes, thats exactly what I was looking for, thanks so much crptcblade. I really appriciat the help.

    One more question on this subject, now using what crptcblade posted above, how could I have that keypress (hotkey) preform an action. So like with crptcblade post, the hotkey was the "k" key. How could I have it so once that "k" key was pressed, something like a message box would appear?

  6. #6
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: [2005] Global Hotkeys

    You would do what is in the WndProc routine. When a hotkey is pressed, the system sends a WM_HOTKEY message to the window that the hotkey is tied to. So you just check for the WM_HOTKEY message, do whatever it is you want to do when it is pressed, then pass the message back along.

    So basically, you would replace the Me.Activate() call in WndProc to whatever it is you want to do.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Global Hotkeys

    Cool. I want to get a chance to use that myself now.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    50

    Re: [2005] Global Hotkeys

    So basically, where it has

    VB Code:
    1. If m.Msg = WM_HOTKEY Then
    2. Me.Activate()
    3. End If

    If I was to put

    VB Code:
    1. If m.Msg = WM_HOTKEY Then
    2. Me.Activate()
    3. MsgBox ("Hi")
    4. End If

    Then when the 'k' key is pressed when on focus of any other application or window then a message box saying "Hi" would appear on my application, is that correct? Or have a got it in the wrong place, because it didn't work for me.

    Oh and thanks mate, your the best, I owe you big time for all this help .

  9. #9
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: [2005] Global Hotkeys

    Note that the messagebox won't appear until the app that fires it gets the focus...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width