Visual Basic

Moderators: None (Apply to moderate this forum)
Number of threads: 17974
Number of posts: 55343

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Press more than 1 key Posted by jeffy_khor on 17 Jan 2002 at 7:38 PM
How to do a program that can detect user to press more than 1 key at the same times. Like the game does. Press 'A' Key and 'B' key at the same times and computer will know there are two keys have been pressed.

Is it posible to do it in VB, and what is the name for this action(pressing 2 key at one times), My english is not very good but I want to try to search in serach engine. Please reply.
Report
Re: Press more than 1 key Posted by KDivad Leahcim on 18 Jan 2002 at 6:08 AM
: How to do a program that can detect user to press more than 1 key at the same times. Like the game does. Press 'A' Key and 'B' key at the same times and computer will know there are two keys have been pressed.
:
: Is it posible to do it in VB, and what is the name for this action(pressing 2 key at one times), My english is not very good but I want to try to search in serach engine. Please reply.
:

Rough example, badly done, but illustrates what you'll need to do.
Dim AlphaDown(0 To 25) As Boolean
....ControlName_KeyDown(KeyCode As Integer, ...)

    If KeyCode <= vbKeyA And KeyCode >= vbKeyZ Then
        AlphaDown(KeyCode - vbKeyA) = True
        'Loop through array and see if other keys are down
    End If

End Sub
....ControlName_KeyUp(KeyCode As Integer, ...)

    If KeyCode <= vbKeyA And KeyCode >= vbKeyZ Then AlphaDown(KeyCode - vbKeyA) = False

End Sub

Report
Re: Press more than 1 key Posted by jeffy_khor on 18 Jan 2002 at 6:57 PM
Sorry for bother you again, but I don't understand the code, I actually is a newbie, can you give me example. Thank you.
Report
Re: Press more than 1 key Posted by phét on 19 Jan 2002 at 12:57 AM
: Sorry for bother you again, but I don't understand the code, I actually is a newbie, can you give me example. Thank you.
:

I recently had to do something similar in a program im working on. I eventually came up with this after studying the msnd librarys a bit.

SendKeys "^{end}"

that sends the combo CTRL + END

here is a short description from the MSDN librarys.

To specify that any combination of SHIFT, CTRL, and ALT should be held down while several other keys are pressed, enclose the code for those keys in parentheses. For example, to specify to hold down SHIFT while E and C are pressed, use "+(EC)". To specify to hold down SHIFT while E is pressed, followed by C without SHIFT, use "+EC".

pht
http://fade.to/phet
12d.hypermart.net
phet@ocis.net

Report
Re: Press more than 1 key Posted by CplPunisher on 19 Jan 2002 at 4:07 AM
There is also an API that makes this a little easier I think...

Public Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Long) As Integer


if you know what combinations of keys you are looking for you can use it like this...

If (GetAsyncKeyState(35) < 1) and (GetAsyncKeyState(42) < 1) then Call DoFunctionOfSomeKind()

I'm not sure what key #42 is, I think 35 is the end key, but you get the idea.... Also When the key is actually pressed I think the value drops below 1, but you might want to play with it a little to see what happens and then rewrite that if/then up there... :) hope that helps.


Report
Re: Press more than 1 key Posted by KDivad Leahcim on 19 Jan 2002 at 6:28 AM
: There is also an API that makes this a little easier I think...
:
:
: Public Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Long) As Integer
: 

:
: if you know what combinations of keys you are looking for you can use it like this...
:
: If (GetAsyncKeyState(35) < 1) and (GetAsyncKeyState(42) < 1) then Call DoFunctionOfSomeKind()
:
: I'm not sure what key #42 is, I think 35 is the end key, but you get the idea.... Also When the key is actually pressed I think the value drops below 1, but you might want to play with it a little to see what happens and then rewrite that if/then up there... :) hope that helps.
:
:
:

0 is up, 1 is up (firing bit active?), -32768 is down, -32767 is down and firing.
Report
Re: Press more than 1 key Posted by KDivad Leahcim on 19 Jan 2002 at 6:31 AM
: Sorry for bother you again, but I don't understand the code, I actually is a newbie, can you give me example. Thank you.
:

Alright, let's start with this: What keys do you wish to watch for? And which keys must be combined?
Report
Re: Press more than 1 key Posted by jacob_miw on 19 Jan 2002 at 8:58 AM
: : Sorry for bother you again, but I don't understand the code, I actually is a newbie, can you give me example. Thank you.
: :
:
: Alright, let's start with this: What keys do you wish to watch for? And which keys must be combined?
:

While your at this subject: I would like to send Alt+Tab when a button is pressed! How??

Thanks

jacob_miw
----------
ICQ# 56564298

Report
Re: Press more than 1 key Posted by CplPunisher on 19 Jan 2002 at 9:13 AM
: : : Sorry for bother you again, but I don't understand the code, I actually is a newbie, can you give me example. Thank you.
: : :
: :
: : Alright, let's start with this: What keys do you wish to watch for? And which keys must be combined?
: :
:
: While your at this subject: I would like to send Alt+Tab when a button is pressed! How??
:
: Thanks
:
: jacob_miw
: ----------
: ICQ# 56564298
:
:

Use the keybd_event API :) that will simulate an actual user key press.

Report
Re: Press more than 1 key Posted by jacob_miw on 19 Jan 2002 at 9:27 AM
Use the keybd_event API :) that will simulate an actual user key press.
:
:

Thanks Cpl, but since Im quite stupid: PLEEEASE give me an example :)
jacob_miw
----------
ICQ# 56564298

Report
Re: Press more than 1 key Posted by CplPunisher on 19 Jan 2002 at 9:42 AM
stick this in a form :)

the key codes are in hex, but I believe it's the same key codes
that the keydown subroutine uses. :) Simple huh?

Good luck

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Sub Form_Load()
Call keybd_event(&H12, &H12, 1, 1) ' ones push it down
doevents
Call keybd_event(&H9, &H9, 1, 1)
DoEvents
Call keybd_event(&H9, &H9, 0, 0) ' and zeros pick it up...
DoEvents
Call keybd_event(&H12, &H12, 0, 0)
DoEvents

End Sub ' &H9 is the tab key and &H12 is the alt key :)
' that should be simple enough... hope that helps...



Report
Re: Press more than 1 key Posted by jacob_miw on 19 Jan 2002 at 10:28 AM
: stick this in a form :)
:
: the key codes are in hex, but I believe it's the same key codes
: that the keydown subroutine uses. :) Simple huh?
:
: Good luck
:
:
: Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
: Private Sub Form_Load()
: Call keybd_event(&H12, &H12, 1, 1) ' ones push it down
: doevents
: Call keybd_event(&H9, &H9, 1, 1)
: DoEvents
: Call keybd_event(&H9, &H9, 0, 0) ' and zeros pick it up...
: DoEvents
: Call keybd_event(&H12, &H12, 0, 0)
: DoEvents
: 
: End Sub ' &H9 is the tab key and &H12 is the alt key :)
: ' that should be simple enough... hope that helps...
: 
: 

:
:

Yeah it should, but it doesn't work :)
When I put the code in a cmdbutton and clicks it the little "dialogbox" shows up (Like when you hold down Alt+Tab), but it doesn't disappear again... I guess something was wrong with the ' and zeros pick it up...

Would you give it another try?

Report
Re: Press more than 1 key Posted by KDivad Leahcim on 20 Jan 2002 at 8:02 AM
: : stick this in a form :)
: :
: : the key codes are in hex, but I believe it's the same key codes
: : that the keydown subroutine uses. :) Simple huh?
: :
: : Good luck
: :
: :
: : Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
: : Private Sub Form_Load()
: : Call keybd_event(&H12, &H12, 1, 1) ' ones push it down
: : doevents
: : Call keybd_event(&H9, &H9, 1, 1)
: : DoEvents
: : Call keybd_event(&H9, &H9, 0, 0) ' and zeros pick it up...
: : DoEvents
: : Call keybd_event(&H12, &H12, 0, 0)
: : DoEvents
: : 
: : End Sub ' &H9 is the tab key and &H12 is the alt key :)
: : ' that should be simple enough... hope that helps...
: : 
: : 

: :
: :
:
: Yeah it should, but it doesn't work :)
: When I put the code in a cmdbutton and clicks it the little "dialogbox" shows up (Like when you hold down Alt+Tab), but it doesn't disappear again... I guess something was wrong with the ' and zeros pick it up...
:
: Would you give it another try?
:
:

Seems to me like the last param should always be 0... Don't know for sure and don't know if it'd make a difference.
Report
Re: Press more than 1 key Posted by CplPunisher on 20 Jan 2002 at 9:10 AM
Boy it sure doesn't pick up very well does it? I'm looking at an alt-tab screen that is stuck on my screen right now...


Uhm... let me check it out...

Report
Re: Press more than 1 key Posted by CplPunisher on 20 Jan 2002 at 9:15 AM
Okay... :) My fault... not testing stuff before I say it again... I'm famous for that...

TWO is up and 1 is down... Zeros don't do anything but make you annoyed that a little alt tab window is on your screen.

:) We can be happy once again.

Report
Re: Press more than 1 key Posted by jeffy_khor on 19 Jan 2002 at 7:23 PM
: : Sorry for bother you again, but I don't understand the code, I actually is a newbie, can you give me example. Thank you.
: :
:
: Alright, let's start with this: What keys do you wish to watch for? And which keys must be combined?
:

I am try to do this program and test it, but it won't work like what I want, would you get a try. Thank you.


Option Explicit
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

Private Sub Form_Activate()
Dim GameOver As Boolean

Do

If GetAsyncKeyState(vbKeyUp) < 0 Then
    Label1.Visible = True
Else
    Label1.Visible = False
End If

If GetAsyncKeyState(vbKeyDown) < 0 Then
    Label2.Visible = True
Else
    Label2.Visible = False
End If

If GetAsyncKeyState(vbKeyLeft) < 0 Then
    Label3.Visible = True
Else
    Label3.Visible = False
End If

If GetAsyncKeyState(vbKeyRight) < 0 Then
    Label4.Visible = True
Else
    Label4.Visible = False
End If

If GetAsyncKeyState(vbKeySpace) < 0 Then
    Label5.Visible = True
Else
    Label5.Visible = False
End If
If GetAsyncKeyState(vbKeyEscape) < 0 Then GameOver = True

DoEvents

Loop Until GameOver = True

End
End Sub




I am try to write a game. And I found it some key doesn't work like what I do. In this program, we can press Up, Down, Left, Right, and space key and it will appear what key we press. It is OK if I try to press up, right and space key, It will show Up, right and space key were pressing. But it won't work if I press Up, left and space. It only show Up and left key. There are some other key when I try to press 3 key in one times and it won't show up all the key. so, please help.



Report
Re: Press more than 1 key Posted by CplPunisher on 20 Jan 2002 at 1:17 AM
: : : Sorry for bother you again, but I don't understand the code, I actually is a newbie, can you give me example. Thank you.
: : :
: :
: : Alright, let's start with this: What keys do you wish to watch for? And which keys must be combined?
: :
:
: I am try to do this program and test it, but it won't work like what I want, would you get a try. Thank you.
:
:
: 
: Option Explicit
: Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
: 
: Private Sub Form_Activate()
: Dim GameOver As Boolean
: 
: Do
: 
: If GetAsyncKeyState(vbKeyUp) < 0 Then
:     Label1.Visible = True
: Else
:     Label1.Visible = False
: End If
: 
: If GetAsyncKeyState(vbKeyDown) < 0 Then
:     Label2.Visible = True
: Else
:     Label2.Visible = False
: End If
: 
: If GetAsyncKeyState(vbKeyLeft) < 0 Then
:     Label3.Visible = True
: Else
:     Label3.Visible = False
: End If
: 
: If GetAsyncKeyState(vbKeyRight) < 0 Then
:     Label4.Visible = True
: Else
:     Label4.Visible = False
: End If
: 
: If GetAsyncKeyState(vbKeySpace) < 0 Then
:     Label5.Visible = True
: Else
:     Label5.Visible = False
: End If
: If GetAsyncKeyState(vbKeyEscape) < 0 Then GameOver = True
: 
: DoEvents
: 
: Loop Until GameOver = True
: 
: End
: End Sub
: 
: 

:
:
: I am try to write a game. And I found it some key doesn't work like what I do. In this program, we can press Up, Down, Left, Right, and space key and it will appear what key we press. It is OK if I try to press up, right and space key, It will show Up, right and space key were pressing. But it won't work if I press Up, left and space. It only show Up and left key. There are some other key when I try to press 3 key in one times and it won't show up all the key. so, please help.
:
:
:
:

Well for both the keybd_event and the Getasynckeystate, I could just be an idiot :) I haven't used them in a while, but they have worked for me before, so if you tinker with them a little bit I'm sure you'll have some luck sooner or later :)>.. I'll tinker with them myself and let you know what I find.

Report
Re: Press more than 1 key Posted by KDivad Leahcim on 20 Jan 2002 at 8:14 AM
: : : Sorry for bother you again, but I don't understand the code, I actually is a newbie, can you give me example. Thank you.
: : :
: :
: : Alright, let's start with this: What keys do you wish to watch for? And which keys must be combined?
: :
:
: I am try to do this program and test it, but it won't work like what I want, would you get a try. Thank you.
:
:
: 
: Option Explicit
: Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
: 
: Private Sub Form_Activate()
: Dim GameOver As Boolean
: 
: Do
: 
: If GetAsyncKeyState(vbKeyUp) < 0 Then
:     Label1.Visible = True
: Else
:     Label1.Visible = False
: End If
: 
: If GetAsyncKeyState(vbKeyDown) < 0 Then
:     Label2.Visible = True
: Else
:     Label2.Visible = False
: End If
: 
: If GetAsyncKeyState(vbKeyLeft) < 0 Then
:     Label3.Visible = True
: Else
:     Label3.Visible = False
: End If
: 
: If GetAsyncKeyState(vbKeyRight) < 0 Then
:     Label4.Visible = True
: Else
:     Label4.Visible = False
: End If
: 
: If GetAsyncKeyState(vbKeySpace) < 0 Then
:     Label5.Visible = True
: Else
:     Label5.Visible = False
: End If
: If GetAsyncKeyState(vbKeyEscape) < 0 Then GameOver = True
: 
: DoEvents
: 
: Loop Until GameOver = True
: 
: End
: End Sub
: 
: 

:
:
: I am try to write a game. And I found it some key doesn't work like what I do. In this program, we can press Up, Down, Left, Right, and space key and it will appear what key we press. It is OK if I try to press up, right and space key, It will show Up, right and space key were pressing. But it won't work if I press Up, left and space. It only show Up and left key. There are some other key when I try to press 3 key in one times and it won't show up all the key. so, please help.
:

Odd... I have no idea why it won't work. All the code looks perfectly OK to me. While I can't fix your code, I can tell you that this version of it is faster, easier to read and won't be as likely to contribute to system crashes:
Private Sub Form_Activate()

    Do Until GetAsyncKeyState(vbKeyEscape) < 0
        Label1.Visible = CBool(GetAsyncKeyState(vbKeyUp) < 0)
        Label2.Visible = CBool(GetAsyncKeyState(vbKeyDown) < 0)
        Label3.Visible = CBool(GetAsyncKeyState(vbKeyLeft) < 0)
        Label4.Visible = CBool(GetAsyncKeyState(vbKeyRight) < 0)
        Label5.Visible = CBool(GetAsyncKeyState(vbKeySpace) < 0)
        DoEvents
    Loop
    'End. NEVER use End. Unload all forms, set all objects to
    'nothing and exit all code. App will end on it's own.
    Unload Me

End Sub

I sincerely doubt my rewrite made a difference, but I tested my code and it detects all the keys properly.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.