| View previous topic :: View next topic |
| Author |
Message |
AndrewMan Grandmaster Cheater Supreme
Reputation: 0
Joined: 01 Aug 2007 Posts: 1257
|
Posted: Thu Jun 26, 2008 12:22 am Post subject: Quick question |
|
|
For hotkeys, where do I put them?
Example:
| Code: | If keys.A Then
Button1.Visible = True
End if |
Where do I put this? Not in the form load...
_________________
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Thu Jun 26, 2008 12:53 am Post subject: |
|
|
You get to venture into the wonderful world of Win32 API / Windows messages for this one.
If you were going to do it like that, you'd need to throw that on the WM_KEYDOWN message for your window. To do that in .NET, you need to override WndProc. This will only work if your window is the active one anyway, so if you're looking for the hotkeys to work at any time, you're better off with...
RegisterHotKey or
GetAsyncKeyState
|
|
| Back to top |
|
 |
Typhoon808 Expert Cheater
Reputation: 0
Joined: 27 Mar 2008 Posts: 175 Location: Wales
|
Posted: Thu Jun 26, 2008 6:36 am Post subject: |
|
|
| You would put that in the Form1_KeyDown event. However, it is best to use an API such as RegisterHotKey as slovach said.
|
|
| Back to top |
|
 |
AndrewMan Grandmaster Cheater Supreme
Reputation: 0
Joined: 01 Aug 2007 Posts: 1257
|
Posted: Thu Jun 26, 2008 9:27 am Post subject: |
|
|
| Typhoon808 wrote: | | You would put that in the Form1_KeyDown event. However, it is best to use an API such as RegisterHotKey as slovach said. |
thanks guys.
_________________
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Thu Jun 26, 2008 7:05 pm Post subject: |
|
|
| Typhoon808 wrote: | | You would put that in the Form1_KeyDown event. However, it is best to use an API such as RegisterHotKey as slovach said. |
That would only work if the form itself has focus then. Typically people would want the hotkey to work with the window not in focus and such as well. Which would put the API in better suite for that purpose.
_________________
- Retired. |
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Thu Jun 26, 2008 7:13 pm Post subject: |
|
|
| Typhoon808 wrote: | | You would put that in the Form1_KeyDown event. However, it is best to use an API such as RegisterHotKey as slovach said. |
Oh man I completely forgot about form events.
Just remember, your form needs to be in focus to get the messages.
|
|
| Back to top |
|
 |
AndrewMan Grandmaster Cheater Supreme
Reputation: 0
Joined: 01 Aug 2007 Posts: 1257
|
Posted: Thu Jun 26, 2008 8:40 pm Post subject: |
|
|
I put my form in focus, but this whole RegisterHotKey thing is so confusing.
I have to import some sort of DLL, than call GlobalAddAtom?
Been Googling quite a bit, there seems that you must do so much work just to get some simple Hotkey working.
_________________
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Fri Jun 27, 2008 9:34 pm Post subject: |
|
|
You don't have to do all that, it is just the proper method of doing it. If it's too confusing, you can always just use GetAsyncKeyState.
_________________
- Retired. |
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Fri Jun 27, 2008 10:34 pm Post subject: |
|
|
| AndrewMan wrote: | I put my form in focus, but this whole RegisterHotKey thing is so confusing.
I have to import some sort of DLL, than call GlobalAddAtom?
Been Googling quite a bit, there seems that you must do so much work just to get some simple Hotkey working. |
pinvoke.net + msdn
declare the pinvoke signature
| Code: | <DllImport("user32.dll")> _
Public Shared Function RegisterHotKey(ByVal hWnd As IntPtr, ByVal id As Integer, _
ByVal fsModifier As Integer, ByVal vk As Integer) As Integer
End Function |
what msdn says.
| Code: | BOOL RegisterHotKey(
HWND hWnd,
int id,
UINT fsModifiers,
UINT vk |
Get the handle of your window: me.Handle();
Set an id, say... 1
No modifiers, 0
Finally use the keys class to get a virtual key.
With your hotkey registered, you can respond to it on WM_HOTKEY by overriding WndProc. When you're done, just UnregisterHotkey.
I'm pulling this part out of my ass as how to actually do it, but it should be close enough
| Code: | int WM_HOTKEY = 0x312;
Protected Overrides Sub WndProc(ByRef m As Message)
Select Case m.Msg
Case WM_HOTKEY Then
m.GetLParam(I'M TOO LAZY TO LOOK UP HOW IT WORKS, so you can do it)
End If
End Sub
|
Or you can just throw GetAsyncKeyState into a thread, loop, timer, whatever.
The whole process is just made needlessly more complicated in managed code IMO.
|
|
| Back to top |
|
 |
|