|
-
Jan 4th, 2008, 08:29 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Universal Key Press
How do I go about making this code work no matter what form is present:
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyF1 Then
Config.Show
End If
End Sub
I have about 30 forms so I don't want to have to add something to every form, is there a way that that code can be modified so that no matter what form is on screen, if I press F1 it will do the command?
-
Jan 4th, 2008, 08:45 PM
#2
Re: Universal Key Press
hmmm, no matter which control on any form has the focus? Brainteaser, a registered hotkey probably wouldn't be a good solution since other apps use F1 for other purposes. Adding a keyboard hook would work, but can be far more hassle than updating the 30 forms. Thinking...
-
Jan 4th, 2008, 08:57 PM
#3
Thread Starter
Hyperactive Member
Re: Universal Key Press
Oh, doesnt have to be F1.. I just used that as an example, bad example; sorry. It can be and F row button..
-
Jan 4th, 2008, 09:12 PM
#4
Re: Universal Key Press
Regarding a hotkey, generally you wouldn't use anything but a combination of keys for fear of interfering with other apps. So, short of modifying all of your forms, you can add this hook. With the hook comes a strong warning:
WARNING (strong). Do not hit END do not press the VB toolbar's STOP button while the hook is running. Though keyboard hooks are less prone to crashing VB IDE in those situations, they can cause your app to stop responding to keyboard activity. Should this happen, close & save your project, close VB and restart VB.
Ok the above being said, I added a constant you can set. Set it to True to allow hooking when in IDE else set it to False. When set to False, it will hook when app is compiled. Set it to True only to test it or test the "hot key" is functioning properly
In the myHookProc function, change vbKeyF1 to the vbKey you want to trap -- one key only. If wanting to trap multiple keys, then a registered hotkey should probably be used.
Ok, in a module add the code below. Required: In your startup form, the last statement in your form load, add: SetKeybdHook. In the form unload, add: ReleaseKeybdHook.
Code:
Option Explicit
Private Const WH_KEYBOARD As Long = 2
Private Declare Function SetWindowsHookEx Lib "user32.dll" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Function CallNextHookEx Lib "user32.dll" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, ByVal lParam As Any) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32.dll" (ByVal hHook As Long) As Long
Private m_HookID As Long
Private Const bHookInIDE As Boolean = False
' ^^ change to True to test functionality & when happy, change back to False
Public Sub SetKeybdHook()
If m_HookID = 0& Then
If bHookInIDE = False Then
On Error Resume Next
Debug.Print 1 / 0 'Debug.Print lines of code are never compiled
If Err Then
Err.Clear ' if err, then we are in ide.
Exit Sub
End If
On Error GoTo 0
End If
m_HookID = SetWindowsHookEx(WH_KEYBOARD, AddressOf myHookProc, App.hInstance, App.ThreadID)
End If
End Sub
Public Sub ReleaseKeybdHook()
If Not m_HookID = 0& Then
UnhookWindowsHookEx m_HookID
m_HookID = 0&
End If
End Sub
Private Function myHookProc(ByVal ncode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'change vbKeyF1 to whatever key you want to trap
If ncode = 0 Then 'HC_ACTION
If wParam = vbKeyF1 Then 'wParam are vbKey... variables
If lParam < 0 Then ' key is up else key is down
If wParam = vbKeyF1 Then
Config.Show ' what to be done when hotkey is pressed
End If
End If
myHookProc = 1 ' eat the key; don't let VB have it
Else
myHookProc = CallNextHookEx(m_HookID, ncode, wParam, lParam)
End If
Else
myHookProc = CallNextHookEx(m_HookID, ncode, wParam, lParam)
End If
End Function
-
Jan 4th, 2008, 11:01 PM
#5
Re: Universal Key Press
LaVolp,
isn't RegisterHotKey a simple solution than #4 ?
-
Jan 4th, 2008, 11:10 PM
#6
Re: Universal Key Press
 Originally Posted by Fazi
LaVolpe,
isn't RegisterHotKey a simple solution than #4 ?
Yes, but not really. Reading the MSDN documentation, can't be a single key, must be a key in combination with alt, ctrl, shift or window key. If that is desired, then it is a better solution. If a single key is needed, then no.
There are pros & cons to both:
1. Keyboard hook not nearly as fatal to IDE
2. For RegisterHotKey messages...
a. Create separate message pump. I think more of a bottleneck for messages than hook
b. Subclass to get the WM_HotKey message. Subclassing is dangerous in IDE
-
Jan 4th, 2008, 11:18 PM
#7
Re: Universal Key Press
Yes,yes, it seems he wanted a single key and not a combination.
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
|