|
-
Dec 22nd, 2006, 07:04 AM
#1
Thread Starter
Member
[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.
-
Dec 22nd, 2006, 07:13 AM
#2
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.
-
Dec 22nd, 2006, 07:29 AM
#3
Thread Starter
Member
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.
-
Dec 22nd, 2006, 12:06 PM
#4
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
-
Dec 22nd, 2006, 10:47 PM
#5
Thread Starter
Member
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?
-
Dec 22nd, 2006, 11:12 PM
#6
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
-
Dec 23rd, 2006, 01:18 AM
#7
Re: [2005] Global Hotkeys
Cool. I want to get a chance to use that myself now.
-
Dec 23rd, 2006, 03:49 AM
#8
Thread Starter
Member
Re: [2005] Global Hotkeys
So basically, where it has
VB Code:
If m.Msg = WM_HOTKEY Then
Me.Activate()
End If
If I was to put
VB Code:
If m.Msg = WM_HOTKEY Then
Me.Activate()
MsgBox ("Hi")
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 .
-
Dec 23rd, 2006, 05:26 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|