Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arrow keys with Command Buttons? 1

Status
Not open for further replies.

Kryzsoccer

Programmer
Oct 15, 2002
85
US
I have created a form that contains many command buttons. On it I would like to do something when the user presses an arrow key. The problem is the arrow keys simply change the selected command button, and they do not trigger any of the KeyDown, KeyPress, or KeyUp events.
Obviously an easy workaround is to change the buttons to labels, but I was wondering if someone had a better solution.
As always ALL help is apreciated!!
 
With API you can register a Hotkey and capture the arrow key code at pressing a command.

peterguhl@yahoo.de
 
This example will capture the left-Arrow Press en command1 object.

Private Const MOD_ALT = &H1
Private Const MOD_CONTROL = &H2
Private Const MOD_SHIFT = &H4
Private Const PM_REMOVE = &H1
Private Const WM_HOTKEY = &H312
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type Msg
hWnd As Long
Message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type
Private Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Private Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long) As Long
Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Private Declare Function WaitMessage Lib "user32" () As Long
Private bCancel As Boolean
Private Sub ProcessMessages()
Dim Message As Msg
Do While Not bCancel
WaitMessage
If PeekMessage(Message, Command1.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
MsgBox "Pressed de Key-Left on Command"
End If
DoEvents
Loop
End Sub
Private Sub Form_Load()
Dim ret As Long
bCancel = False
ret = RegisterHotKey(Command1.hWnd, &HBFFF&, &H0, vbKeyLeft)
Me.AutoRedraw = True
Show
ProcessMessages
End Sub
Private Sub Form_Unload(Cancel As Integer)
bCancel = True
Call UnregisterHotKey(Command1.hWnd, &HBFFF&)
End Sub



peterguhl@yahoo.de
 
This example will capture the four arrow-buttons,
Think it will help you.

Private Const MOD_ALT = &H1
Private Const MOD_COMUN = &H0
Private Const MOD_CONTROL = &H2
Private Const MOD_SHIFT = &H4
Private Const PM_REMOVE = &H1
Private Const WM_HOTKEY = &H312
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type Msg
hWnd As Long
Message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type
Private Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Private Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long) As Long
Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Private Declare Function WaitMessage Lib "user32" () As Long
Private bCancel As Boolean


Private Sub Command1_GotFocus()
Dim Message As Msg
Dim ret As Long
bCancel = False
ret = RegisterHotKey(Command1.hWnd, &HBFFF&, MOD_COMUN, vbKeyLeft)
ret = RegisterHotKey(Command1.hWnd, &HBFFE&, MOD_COMUN, vbKeyRight)
ret = RegisterHotKey(Command1.hWnd, &HBFFD&, MOD_COMUN, vbKeyUp)
ret = RegisterHotKey(Command1.hWnd, &HBFFC&, MOD_COMUN, vbKeyDown)

Do While Not bCancel
WaitMessage
If PeekMessage(Message, Command1.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
Select Case Message.wParam
Case &HBFFF&
MsgBox "Pressed de Key-Left on Command"
Case &HBFFE&
MsgBox "Pressed de Key-Right on Command"
Case &HBFFD&
MsgBox "Pressed de Key-Up on Command"
Case &HBFFC&
MsgBox "Pressed de Key-Down on Command"
End Select
End If
DoEvents
Loop

End Sub

Private Sub Command1_LostFocus()
bCancel = True
Call UnregisterHotKey(Command1.hWnd, &HBFFF&)
Call UnregisterHotKey(Command1.hWnd, &HBFFE&)
Call UnregisterHotKey(Command1.hWnd, &HBFFD&)
Call UnregisterHotKey(Command1.hWnd, &HBFFC&)
End Sub

Private Sub Form_Load()
Command1.Caption = "Test"
Command2.Caption = "Commun"
'ProcessMessages
End Sub

thk for req



peterguhl@yahoo.de
 
Thanks for the quick responses. A star for you. I modified the last code you posted a little bit to make it work with all the command buttons and everything works great.
Just out of curiosity, why do you unregister the keys when the button loses focus? Would anything change/happen if the hotkey is left unregistered when the program is ended?

Thanks for the help!
 
The hWnd Property is a relativ Relation so if you open and clos a form it can be that an other Control will have the HotKey installed.


peterguhl@yahoo.de
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top