Results 1 to 16 of 16

Thread: [RESOLVED] Get VBKEY

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    122

    Resolved [RESOLVED] Get VBKEY

    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

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Get VBKEY

    something like:
    VB Code:
    1. ' Set form's KeyPreview property to True
    2. Private cButton As CommandButton
    3.  
    4. Private Sub Command1_Click()
    5.     Set cButton = Command1
    6. End Sub
    7.  
    8. Private Sub KeyPress(KeyAscii As Integer)
    9.     If Not cButton Is Nothing Then
    10.         cButton.Caption = Chr$(KeyAscii)
    11.         Set cButton = Nothing
    12.     End If
    13. End Sub
    i've done it that way so you can use the same code for other buttons.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    122

    Re: Get VBKEY

    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?

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Get VBKEY

    ahhh, well those are things you should have said first time around.

    In a form:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Set cButton = Command1
    3.     SetKeyboardHook
    4. End Sub
    5.  
    6. Private Sub Form_Unload(Cancel As Integer)
    7.     RemoveKeyboardHook
    8. End Sub
    In a module
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" ( _
    4.     ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
    5.  
    6. Private Declare Function UnhookWindowsHookEx Lib "user32" ( _
    7.     ByVal hHook As Long) As Long
    8.  
    9. Private Declare Function CallNextHookEx Lib "user32" ( _
    10.     ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
    11.    
    12. Private Declare Function MapVirtualKey Lib "user32.dll" Alias "MapVirtualKeyA" ( _
    13.     ByVal wCode As Long, ByVal wMapType As Long) As Long
    14.        
    15. Private Declare Function GetKeyNameText Lib "user32.dll" Alias "GetKeyNameTextA" ( _
    16.     ByVal lParam As Long, ByVal lpBuffer As String, ByVal nSize As Long) As Long
    17.  
    18. Private Const WH_KEYBOARD_LL = 13
    19. Private Const HC_ACTION = 0
    20.  
    21. Private Type KBDLLHOOKSTRUCT
    22.     vkCode As Long
    23.     scanCode As Long
    24.     flags As Long
    25.     time As Long
    26.     dwExtraInfo As Long
    27. End Type
    28.  
    29. Private hHook As Long
    30. Private IsHooked As Boolean
    31. Public cButton As CommandButton
    32.  
    33. Public Sub SetKeyboardHook()
    34.     If Not IsHooked Then
    35.         hHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, App.hInstance, 0&)
    36.         IsHooked = True
    37.     End If
    38. End Sub
    39.  
    40. Public Sub RemoveKeyboardHook()
    41.     If IsHooked Then
    42.         UnhookWindowsHookEx hHook
    43.         IsHooked = False
    44.     End If
    45. End Sub
    46.  
    47. Private Function LowLevelKeyboardProc(ByVal uCode As Long, ByVal wParam As Long, lParam As KBDLLHOOKSTRUCT) As Long
    48.     Dim sBuffer As String, lRet As Long
    49.     If uCode >= 0 Then
    50.         If uCode = HC_ACTION Then
    51.             sBuffer = String$(255, vbNullChar)
    52.             lRet = GetKeyNameText((lParam.scanCode * &H10000) Or (lParam.flags * &H1000000), sBuffer, 255&)
    53.             If Not cButton Is Nothing Then cButton.Caption = Left$(sBuffer, lRet)
    54.             RemoveKeyboardHook
    55.         End If
    56.     End If
    57.     LowLevelKeyboardProc = CallNextHookEx(hHook, uCode, wParam, lParam)
    58. End Function
    be 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!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    122

    Re: Get VBKEY

    Thank you :-)
    One last question and this should be solved
    I'm using the code
    VB Code:
    1. Public Function KeyDown(ByVal vKey As KeyCodeConstants) As Boolean
    2.    On Error Resume Next
    3.    KeyDown = GetAsyncKeyState(vKey) And &H8000
    4.    'If KeyDown = False And vKey > 256 Then
    5.    'If js.Buttons(CByte(vKey - 257)) = 128 Then KeyDown = True
    6.    'End If
    7. End Function
    I tried doing
    VB Code:
    1. Private Sub Timer1_Timer()
    2. Dim keystr As String
    3. keystr = "vbkey" & Command1.Caption
    4. If KeyDown(keystr) Then
    5. MsgBox ("hey")
    6. End If
    7. End Sub

    But no luck, can you instruct me on how to get this working? Thanks for all the help

  6. #6
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    348

    Re: Get VBKEY

    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:
    1. Private Declare Function GetAsyncKeyState Lib "user32" Alias _ "GetAsyncKeyState" (ByVal vKey As Long) As Integer
    2.  
    3. Private Sub Form_load()
    4. timer1.enabled = true
    5. timer1.inverval = 1
    6. end sub
    7.  
    8. Private Sub Timer1_timer()
    9. For i = 1 to 255
    10.  
    11.     If getasynckeystate(i) then
    12.  
    13.         command1.caption = chr(i)
    14.  
    15. end if
    16.  
    17. next i
    18.  
    19. on error resume next
    20.  
    21. end sub

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    122

    Re: Get VBKEY

    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

  8. #8
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    348

    Re: Get VBKEY

    Whats the problem your having with it?

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    122

    Re: Get VBKEY

    Well I'm trying to do something like
    VB Code:
    1. Private Sub Timer1_Timer()
    2. Dim keystr As String
    3. keystr = "vbkey" & Command1.Caption
    4. If KeyDown(keystr) Then
    5. MsgBox ("hey")
    6. End If
    7. 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

  10. #10
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Get VBKEY

    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.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    122

    Re: Get VBKEY

    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

  12. #12
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Get VBKEY

    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:
    VB Code:
    1. ' in LowLevelKeyboardProc
    2.             If Not cButton Is Nothing Then
    3.                 cButton.Caption = Left$(sBuffer, lRet)
    4.                 cButton.Tag = CStr(lParam.vkCode)
    5.             End If
    CLng(Command1.Tag) is the value you need pass as the vk parameter of the RegisterHotKey API

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    122

    Re: Get VBKEY

    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
    VB Code:
    1. RegisterHotKey(Me.hWnd, &HBFFF&, MOD_CONTROL, CLng(Command1.Tag))
    It could be like
    VB Code:
    1. RegisterHotKey(Me.hWnd, &HBFFF&,vbullstring, 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 hotkey

  14. #14
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Get VBKEY

    VB Code:
    1. RegisterHotKey(Me.hWnd, &HBFFF&, 0&, CLng(Command1.Tag))

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    122

    Re: Get VBKEY

    thank you for all of that help, problem solved

  16. #16
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: [RESOLVED] Get VBKEY

    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
    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 Structure

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width