Hey, I'm trying to get the key that a person presses after they've clicked on my command button, so they click on command1, then hit a key, then command1.caption = that key. Can anybody help me with this? Thanks :wave:
Printable View
Hey, I'm trying to get the key that a person presses after they've clicked on my command button, so they click on command1, then hit a key, then command1.caption = that key. Can anybody help me with this? Thanks :wave:
something like:i've done it that way so you can use the same code for other buttons.VB Code:
' Set form's KeyPreview property to True Private cButton As CommandButton Private Sub Command1_Click() Set cButton = Command1 End Sub Private Sub KeyPress(KeyAscii As Integer) If Not cButton Is Nothing Then cButton.Caption = Chr$(KeyAscii) Set cButton = Nothing End If End Sub
hmm for some reason it's not working for me, i got it working when it was Form1_KeyPress, but my form won't always be the topmost window so i can't really do that. Also some keys such as 'shift', 'home', and 'end' dont work. Thanks for the help though, any other ideas?
ahhh, well those are things you should have said first time around.
In a form:In a moduleVB Code:
Private Sub Command1_Click() Set cButton = Command1 SetKeyboardHook End Sub Private Sub Form_Unload(Cancel As Integer) RemoveKeyboardHook End Subbe aware that any unhandled errors (that includes pressing Stop in the IDE, or attempting to debug) that occur between when the Hook is set and when it is removed will result in the program (and the IDE) to bomb out - save often!VB Code:
Option Explicit Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" ( _ ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long Private Declare Function UnhookWindowsHookEx Lib "user32" ( _ ByVal hHook As Long) As Long Private Declare Function CallNextHookEx Lib "user32" ( _ ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long Private Declare Function MapVirtualKey Lib "user32.dll" Alias "MapVirtualKeyA" ( _ ByVal wCode As Long, ByVal wMapType As Long) As Long Private Declare Function GetKeyNameText Lib "user32.dll" Alias "GetKeyNameTextA" ( _ ByVal lParam As Long, ByVal lpBuffer As String, ByVal nSize As Long) As Long Private Const WH_KEYBOARD_LL = 13 Private Const HC_ACTION = 0 Private Type KBDLLHOOKSTRUCT vkCode As Long scanCode As Long flags As Long time As Long dwExtraInfo As Long End Type Private hHook As Long Private IsHooked As Boolean Public cButton As CommandButton Public Sub SetKeyboardHook() If Not IsHooked Then hHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, App.hInstance, 0&) IsHooked = True End If End Sub Public Sub RemoveKeyboardHook() If IsHooked Then UnhookWindowsHookEx hHook IsHooked = False End If End Sub Private Function LowLevelKeyboardProc(ByVal uCode As Long, ByVal wParam As Long, lParam As KBDLLHOOKSTRUCT) As Long Dim sBuffer As String, lRet As Long If uCode >= 0 Then If uCode = HC_ACTION Then sBuffer = String$(255, vbNullChar) lRet = GetKeyNameText((lParam.scanCode * &H10000) Or (lParam.flags * &H1000000), sBuffer, 255&) If Not cButton Is Nothing Then cButton.Caption = Left$(sBuffer, lRet) RemoveKeyboardHook End If End If LowLevelKeyboardProc = CallNextHookEx(hHook, uCode, wParam, lParam) End Function
Thank you :-)
One last question and this should be solved
I'm using the code
I tried doingVB Code:
Public Function KeyDown(ByVal vKey As KeyCodeConstants) As Boolean On Error Resume Next KeyDown = GetAsyncKeyState(vKey) And &H8000 'If KeyDown = False And vKey > 256 Then 'If js.Buttons(CByte(vKey - 257)) = 128 Then KeyDown = True 'End If End Function
VB Code:
Private Sub Timer1_Timer() Dim keystr As String keystr = "vbkey" & Command1.Caption If KeyDown(keystr) Then MsgBox ("hey") End If End Sub
But no luck, can you instruct me on how to get this working? Thanks for all the help
This will change it to whatever was last pressed but i dont know how to get do it just once after command click.
VB Code:
Private Declare Function GetAsyncKeyState Lib "user32" Alias _ "GetAsyncKeyState" (ByVal vKey As Long) As Integer Private Sub Form_load() timer1.enabled = true timer1.inverval = 1 end sub Private Sub Timer1_timer() For i = 1 to 255 If getasynckeystate(i) then command1.caption = chr(i) end if next i on error resume next end sub
Thanks, but that was already solved lol ;-). The problem now is using my Keydown function along with the command1's key caption. Thanks for the help though
Whats the problem your having with it?
Well I'm trying to do something like
VB Code:
Private Sub Timer1_Timer() Dim keystr As String keystr = "vbkey" & Command1.Caption If KeyDown(keystr) Then MsgBox ("hey") End If End Sub
So if they press the key that is set to command1.caption itll execute my code which is in this case MsgBox ('hey'). So basically I'm creating a hotkey that the user can change to whatever they like. I am able to set it now thanks to bushmobile, but now I need to figure out how to use my keydown function in relation to command1.caption because that code i showed above gives an error because that was just a guess at how to do it, but obviously wrong
that's not the way to go about setting up a hotkey - you need to use the RegisterHotKey API - search and i'm sure you'll find lots of examples.
alright, thanks i searched around and i understand that function, but i still dont understand how i would go about using the hotkey defined in command1.caption with it
you won't be able to do it with the caption because you want the caption to hold the button text (not the virtual keycode) - instead change the .Tag property to represent the VK:CLng(Command1.Tag) is the value you need pass as the vk parameter of the RegisterHotKey APIVB Code:
' in LowLevelKeyboardProc If Not cButton Is Nothing Then cButton.Caption = Left$(sBuffer, lRet) cButton.Tag = CStr(lParam.vkCode) End If
Thank you :-) All is working well. One last question. Is there anyway I can make it so I can make only one button a hotkey so instead of
It could be likeVB Code:
RegisterHotKey(Me.hWnd, &HBFFF&, MOD_CONTROL, CLng(Command1.Tag))
(which doesnt work) But what I'm saying is make it have no modifiers, so I could just hit 'Home' for example and that would be the hotkeyVB Code:
RegisterHotKey(Me.hWnd, &HBFFF&,vbullstring, CLng(Command1.Tag))
VB Code:
RegisterHotKey(Me.hWnd, &HBFFF&, 0&, CLng(Command1.Tag))
thank you for all of that help, problem solved
well, if it's not working then you might have to rely on a keyboard hook (as in post #4) and watch for the key combinations you're interested in. I recommend reading the following MSDN articles: LowLevelKeyboardProc Function, KBDLLHOOKSTRUCT StructureQuote:
Sorry, I hope you don't mind me sending this to you, I didnt want to start up a new topic just for this
I just wanted to see if you knew if the hotkeys using registerhotkey work when a fullscreengame is up and running. they don't seem to for me
if you know how to make them work please let me know
or if they don't is there anyway i can use the command1.tag in relation with my keydown function because that does seem to work when a full screen game is up
thanks