|
-
Jan 20th, 2007, 01:32 PM
#1
Thread Starter
Lively Member
[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
-
Jan 20th, 2007, 01:43 PM
#2
Re: Get VBKEY
something like:
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
i've done it that way so you can use the same code for other buttons.
-
Jan 20th, 2007, 02:03 PM
#3
Thread Starter
Lively Member
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?
-
Jan 20th, 2007, 07:42 PM
#4
Re: Get VBKEY
ahhh, well those are things you should have said first time around.
In a form:
VB Code:
Private Sub Command1_Click()
Set cButton = Command1
SetKeyboardHook
End Sub
Private Sub Form_Unload(Cancel As Integer)
RemoveKeyboardHook
End Sub
In a module
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
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!
-
Jan 20th, 2007, 09:32 PM
#5
Thread Starter
Lively Member
Re: Get VBKEY
Thank you :-)
One last question and this should be solved
I'm using the code
VB 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
I tried doing
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
-
Jan 20th, 2007, 09:40 PM
#6
Hyperactive Member
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:
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
-
Jan 20th, 2007, 09:43 PM
#7
Thread Starter
Lively Member
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
-
Jan 20th, 2007, 10:00 PM
#8
Hyperactive Member
Re: Get VBKEY
Whats the problem your having with it?
-
Jan 20th, 2007, 10:05 PM
#9
Thread Starter
Lively Member
Re: Get VBKEY
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
-
Jan 20th, 2007, 10:10 PM
#10
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.
-
Jan 20th, 2007, 10:17 PM
#11
Thread Starter
Lively Member
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
-
Jan 20th, 2007, 10:31 PM
#12
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:
' in LowLevelKeyboardProc
If Not cButton Is Nothing Then
cButton.Caption = Left$(sBuffer, lRet)
cButton.Tag = CStr(lParam.vkCode)
End If
CLng(Command1.Tag) is the value you need pass as the vk parameter of the RegisterHotKey API
-
Jan 20th, 2007, 11:15 PM
#13
Thread Starter
Lively Member
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:
RegisterHotKey(Me.hWnd, &HBFFF&, MOD_CONTROL, CLng(Command1.Tag))
It could be like
VB Code:
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
-
Jan 21st, 2007, 07:51 AM
#14
Re: Get VBKEY
VB Code:
RegisterHotKey(Me.hWnd, &HBFFF&, 0&, CLng(Command1.Tag))
-
Jan 21st, 2007, 10:37 AM
#15
Thread Starter
Lively Member
Re: Get VBKEY
thank you for all of that help, problem solved
-
Jan 21st, 2007, 01:13 PM
#16
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|