andrewo 06-21-2001, 07:16 AM I noticed the other keypress forms are complicated, well I wanted to put up a simple answer for a simple command. This is very easy to use and if you cant get it post a message and I will tell u how.
Option Explicit
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Const KEY_TOGGLED As Integer = &H1
Private Const KEY_DOWN As Integer = &H1000
Private Sub Timer1_Timer()
If (GetKeyState(vbKeyG) And KEY_DOWN) Then Print "key G or g has been pressed"
If (GetKeyState(vbKeyH) And KEY_DOWN) Then Print "key h or H has been pressed"
End Sub
BillSoo 06-21-2001, 05:03 PM I've used a similar method as well. It has the advantage that you can detect when several keys are depressed simultaneously; for instance, the left and up arrows...
But it has some disadvantages as well....
1) if the timer interval is too long, you can miss some quick keystrokes
2) if the timer interval is too short, you spend most of your time in this event
3) timers are a limited resource. If you can, you should minimize the number you have...
You could rewrite this to use a tight loop:
Do
if GetInputState() then 'keyboard or mouse activity...
'detect specific keys here...
doevents
endif
'other code here
Loop until bDone
Which would solve 1 and 3, and if this is your main loop anyway, 2 doesn't matter.
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
andrewo 07-15-2001, 10:54 PM Heres the code i am using..
its says that the "GetKeyState" compile error: argument not optional
Do
If GetKeyState Then 'keyboard or mouse activity...
'detect specific keys here...
If (GetKeyState(vbKeyA) And KEY_DOWN) Then Print "Dd"
If (GetKeyState(vbKeyS) And KEY_DOWN) Then Print "e"
DoEvents
End If
'other code here
Loop Until bDone
~
You are getting that error because the function requires a virtual key as an arguement. I don't know that API very well but I believe you need to send it the key you want to check on. I am not sure if it will report generic keyboard activity.
JDT
andrewo 07-15-2001, 11:31 PM Im not really good with loops so could some1 give me an example of a loop with pressing keys?????
~
BillSoo 07-16-2001, 12:10 AM This line:
If GetKeyState Then 'keyboard or mouse activity...
Should be:
If GetInputState() Then
GetInputState is another API call that evaluates to true if there are mouse or keyboard events pending in the queue.
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
andrewo 07-16-2001, 01:10 AM It now says "sub function not defined" on
if GetInputState()
~
Did you add the API declaration to the general section of the form or in a bas module? If not then add this:
<pre><font color=blue>Private <font color=blue>Declare</font color=blue> Function</font color=blue> GetInputState <font color=blue>Lib</font color=blue> "user32" () <font color=blue>As Long</font color=blue></pre>
JDT
andrewo 07-16-2001, 05:52 AM Ok i got that problem working now. It now says
"Compile Error:variable not defined"
on
bDone
~
BillSoo 07-16-2001, 10:22 AM Obviously one of your variables is not defined. It's probably in one of your other functions though...one that you don't normally run in IDE mode. Maybe even some "dead" code that can't possibly run, but will still cause a compile error.
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
andrewo 07-17-2001, 12:19 AM Im using the code that u used Billsoo, so i dunno
Well this is my main problem
This is for a ping pong game
i want the ball moved by loops to make it look smoother when moving
But the problem with that is it stops me from moving my Bats.
I want to be able to have the ball moving on a loop as well as being able to use the keyboard to move the bats side to side.
~
BillSoo 07-17-2001, 01:05 AM Post your code....
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
andrewo 07-17-2001, 02:24 AM Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Declare Function GetInputState Lib "user32" () As Long
Private Const KEY_TOGGLED As Integer = &H1
Private Const KEY_DOWN As Integer = &H1000
Private Sub Command1_Click()
Do
If GetInputState() Then
'detect specific keys here...
If (GetKeyState(vbKeyA) And KEY_DOWN) Then Print "Dd"
If (GetKeyState(vbKeyS) And KEY_DOWN) Then Print "e"
DoEvents
End If
'other code here
Loop Until bDone
End Sub
~
BillSoo 07-17-2001, 02:25 AM You haven't declared bDone.
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
andrewo 07-19-2001, 08:51 AM what code do i type in?
~
You need to dim the var bDone as Boolean. Try this:
<pre><font color=blue>Option Explicit</font color=blue>
<font color=blue>Private <font color=blue>Declare</font color=blue> Sub</font color=blue> Sleep <font color=blue>Lib</font color=blue> "kernel32" (<font color=blue>ByVal</font color=blue> dwMilliseconds <font color=blue>As Long</font color=blue>)
<font color=blue>Private <font color=blue>Declare</font color=blue> Function</font color=blue> GetKeyState <font color=blue>Lib</font color=blue> "user32" (<font color=blue>ByVal</font color=blue> nVirtKey <font color=blue>As Long</font color=blue>) <font color=blue>As Integer</font color=blue>
<font color=blue>Private <font color=blue>Declare</font color=blue> Function</font color=blue> GetInputState <font color=blue>Lib</font color=blue> "user32" () <font color=blue>As Long</font color=blue>
<font color=blue>Private Const</font color=blue> KEY_TOGGLED <font color=blue>As Integer</font color=blue> = &H1
<font color=blue>Private Const</font color=blue> KEY_DOWN <font color=blue>As Integer</font color=blue> = &H1000
<font color=blue>Private Sub</font color=blue> Command1_Click()
<font color=blue>Dim</font color=blue> bDone <font color=blue>As Boolean</font color=blue>
<font color=blue>Do</font color=blue>
<font color=blue>If</font color=blue> GetInputState() <font color=blue>Then</font color=blue>
<font color=green>'detect specific keys here...</font color=green>
<font color=blue>If</font color=blue> (GetKeyState(vbKeyA) <font color=blue>And</font color=blue> KEY_DOWN) <font color=blue>Then</font color=blue> <font color=blue>Print</font color=blue> "Dd"
<font color=blue>If</font color=blue> (GetKeyState(vbKeyS) <font color=blue>And</font color=blue> KEY_DOWN) <font color=blue>Then</font color=blue> <font color=blue>Print</font color=blue> "e"
DoEvents
<font color=blue>End If</font color=blue>
<font color=green>'other code here</font color=green>
<font color=blue>Loop Until</font color=blue> bDone
<font color=blue>End Sub</font color=blue></pre>
JDT
BillSoo 07-19-2001, 12:05 PM I can't say exactly because it depends on how you set up your program....
But basically, bDone is a BOOLEAN variable that is initially set to FALSE. When you want to break out of the loop, you set the value to TRUE.
For instance, in the form declarations, you could declare bDone like:
dim bDone as Boolean
Then in the form_load event you could have
bDone = FALSE 'yeah I know, it is already false by default but this makes it clear
And in the form_unload or form_queryunload event you could have
bDone = TRUE 'this will cause the loop to exit and allow the program to end.
But you could also set bDone elsewhere...for instance you could have a "cancel" button.
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
|