|
-
Feb 28th, 2002, 08:26 PM
#1
Thread Starter
New Member
help with VK keys
Hey, first of all, is VK key the best way to go? I use it in C++ a litte, seems to work. Anyways, heres what I have, isn't working.
Dim checkkeys As Boolean
Private Declare Function GetAsyncKeyState _
Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Form_Load()
checkstuff
End Sub
Private Sub checkstuff()
checkkeys = True
Do While checkkeys = True
If GetAsyncKeyState(VK_DOWN) Then
MsgBox "YESSUM!"
Else
MsgBox "NOPE"
End If
Loop
End Sub
-
Mar 1st, 2002, 11:15 AM
#2
Frenzied Member
Huh try putting a DoEvents inside the loop... this way it won't stop the program to run the loop. You must also use Me.Show before the loop or it won't display the form properly
-
Mar 1st, 2002, 09:14 PM
#3
Thread Starter
New Member
still nope
still freezes. well, any help is appreciated.
-
Mar 3rd, 2002, 12:22 PM
#4
Frenzied Member
Man that's not a freeze, it's executing the loop just like you told it to 
The problem is that every milisecond or so it checks if the key is pressed, and the messagebox will stop the loop until you press OK and then the loop will start again just a few miliseconds later... and I don't think you can press the down key that fast 
Try this instead. I only did the modifications I told you about, and replaced the messagebox thingy with a line that will simply set the form's caption to the return value.
VB Code:
Dim checkkeys As Boolean
Private Declare Function GetAsyncKeyState _
Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Form_Load()
Me.Show
checkstuff
End Sub
Private Sub checkstuff()
checkkeys = True
Do While checkkeys = True
If GetAsyncKeyState(VK_DOWN) Then
Me.Caption = "YESSUM!"
Else
Me.Caption "NOPE"
End If
DoEvents
Loop
End Sub
(Btw, you can use [ vbcode ] and [/ vbcode ] tags to make your code look better when you post it )
-
Mar 4th, 2002, 08:18 PM
#5
Thread Starter
New Member
lol that doesnt work cuz too fast.
is there any way to tell it to wait 100 ms before checking again? I tried to use a timer but I couldnt figure out how to use it the way I need.
-
Mar 4th, 2002, 09:11 PM
#6
Good Ol' Platypus
Yes, you can limit the loop by using then GetTickCount() API or the QueryPerformanceCounter() API. For example:[Highlight=VB]Private Declare Function GetTickCount Lib "kernel32" Alias "GetTickCount" () As Long
Dim checkkeys As Boolean
Private Declare Function GetAsyncKeyState _
Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Form_Load()
Me.Show
checkstuff
End Sub
Private Sub checkstuff()
Dim T As Long
T = GetTickCount
checkkeys = True
Do While checkkeys = True
If GetTickCount - T => 100 Then 'If 100ms has passed...
If GetAsyncKeyState(VK_DOWN) Then
Me.Caption = "YESSUM!"
Else
Me.Caption "NOPE"
End If
T = GetTickCount
End If
DoEvents
Loop
End Sub
All contents of the above post that aren't somebody elses are mine, not the property of some media corporation. 
(Just a heads-up)
-
Mar 4th, 2002, 10:55 PM
#7
Thread Starter
New Member
nope... hmmm
um its still not working.. this tick count thing works! but.. its not catching the key down..
could it be because I have a usb keyboard? I don't know.. well plz help!
-
Mar 5th, 2002, 05:07 AM
#8
Addicted Member
That's making it only check every 100ms, so it'd be harder to catch keypreses that way.
"1 4m 4 1337 #4xz0r!'
Janus
-
Mar 5th, 2002, 12:00 PM
#9
-
Mar 5th, 2002, 12:09 PM
#10
Fanatic Member
VB Code:
Dim checkkeys As Boolean
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Function GetTickCount Lib "kernel32" Alias "GetTickCount" () As Long
Private Sub Form_Load()
Me.Show
checkstuff
End Sub
Private Sub checkstuff()
Dim tmpL&, bolOn as boolean
checkkeys = True
Do While checkkeys = True
if not bolOn then bolOn =GetAsyncKeyState(VK_DOWN)
if (tmpl-gettickcount) >=100 then
If bolOn Then
Me.Caption = "YESSUM!"
bolOn = false
Else
Me.Caption "NOPE"
End If
tmpl=gettickcount
End if
DoEvents
Loop
End Sub
This will still check every few milliseconds. If it is off, then it will set it to the key state. If it is on, it will leave it on. Then, every 100 milliseconds, it will check the state of the boolean. It will also do the doevents every few milliseconds.
The spelling may not be correct.
I am at school, and can't test the code right now.
-
Mar 5th, 2002, 07:48 PM
#11
Thread Starter
New Member
nope...
I dunno, I think its just not detecting the key.. even the VB key stuff doesnt work. Well I have XP and a USB keyboard, can this effect the well being of the code? In anycase it isn't working so again any help as well as the help I have been getting is welcome and thanked. Thanks guys.
-
Mar 5th, 2002, 08:00 PM
#12
Fanatic Member
Oops, I had the gettickcount and tmpl revesed. Heres the fixed code:
VB Code:
Option Explicit
Dim checkkeys As Boolean
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Form_Load()
Me.Show
DoEvents
checkstuff
End Sub
Private Sub checkstuff()
Dim tmpL&, bolOn As Boolean
checkkeys = True
Do While checkkeys = True
If Not bolOn Then bolOn = GetAsyncKeyState(vbKeyA)
If (GetTickCount - tmpL) >= 100 Then
If bolOn Then
Me.Caption = "YESSUM!"
bolOn = False
Else
Me.Caption = "NOPE"
End If
tmpL = GetTickCount
End If
DoEvents
Loop
Unload Me
End Sub
Private Sub Form_Unload(Cancel As Integer)
If checkkeys Then
checkkeys = False
Cancel = -1
End If
End Sub
This code will also allow the close button to work too.
-
Mar 5th, 2002, 08:01 PM
#13
Fanatic Member
Also, I switched the vbKeyDown to vbKeyA, switch it back if you wish...
So like this:
VB Code:
Option Explicit
Dim checkkeys As Boolean
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Form_Load()
Me.Show
DoEvents
checkstuff
End Sub
Private Sub checkstuff()
Dim tmpL&, bolOn As Boolean
checkkeys = True
Do While checkkeys = True
If Not bolOn Then bolOn = GetAsyncKeyState(vbKeyDown)
If (GetTickCount - tmpL) >= 100 Then
If bolOn Then
Me.Caption = "YESSUM!"
bolOn = False
Else
Me.Caption = "NOPE"
End If
tmpL = GetTickCount
End If
DoEvents
Loop
Unload Me
End Sub
Private Sub Form_Unload(Cancel As Integer)
If checkkeys Then
checkkeys = False
Cancel = -1
End If
End Sub
-
Mar 5th, 2002, 08:09 PM
#14
Thread Starter
New Member
sweet thanks!
Ur the man! Hey what does option explicit do?
-
Mar 5th, 2002, 08:10 PM
#15
Fanatic Member
And heres even more advanced code, which allows for any number of keys (it is set for the four arrow keys).
VB Code:
Option Explicit
Dim checkkeys As Boolean
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Form_Load()
Me.Show
DoEvents
checkstuff
End Sub
Private Sub checkstuff()
Dim tmpL&, bolDown As Boolean, bolUp As Boolean, bolLeft As Boolean, bolRight As Boolean
Dim tmpS$, tmpL2&
checkkeys = True
Do While checkkeys = True
If Not bolDown Then bolDown = GetAsyncKeyState(vbKeyDown)
If Not bolUp Then bolUp = GetAsyncKeyState(vbKeyUp)
If Not bolLeft Then bolLeft = GetAsyncKeyState(vbKeyLeft)
If Not bolRight Then bolRight = GetAsyncKeyState(vbKeyRight)
If (GetTickCount - tmpL) >= 100 Then
tmpS = ""
If bolDown Then
tmpS = tmpS & "Down"
bolDown = False
End If
If bolUp Then
tmpS = tmpS & " Up"
bolUp = False
End If
If bolLeft Then
tmpS = tmpS & " Left"
bolLeft = False
End If
If bolRight Then
tmpS = tmpS & " Right"
bolRight = False
End If
If tmpS = "" Then
tmpS = "None"
End If
tmpS = Trim$(tmpS)
tmpL = GetTickCount
End If
Me.Caption = tmpS
DoEvents
Loop
Unload Me
End Sub
Private Sub Form_Unload(Cancel As Integer)
If checkkeys Then
checkkeys = False
Cancel = -1
End If
End Sub
-
Mar 5th, 2002, 08:13 PM
#16
Fanatic Member
Option Explicit is to make sure that all the varibles are defined. It is a good idea to set it to defualt. Look under tools-options-editor-require varible declaration. That is where it is in 6.0. It is to prevent errors from the wrong types of varibles being declared. Such as you need a string, and it defines it as an interger, or you need a long and it sets it to a single. It also increases speed, as you are less likely to have vareints in the program.
-
Mar 6th, 2002, 06:21 AM
#17
-
Mar 6th, 2002, 10:22 AM
#18
Fanatic Member
I was going to do that, but I figured that it would be too much work to convert it over. And if he really needed that he could ask.
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
|