detecting keypresses in vb.net

Talk about your favorite PC games, Steam and building awesome custom rigs!

Moderator: Moderators

Post Reply
lifeisbetterwithketchup
Senior Member
Posts: 2180
Joined: Fri Jul 21, 2006 12:08 pm
Steam ID: lifeisbetterwithketchup
Location: Illinois. Whee.

detecting keypresses in vb.net

Post by lifeisbetterwithketchup »

OK, I knew how to do this a few weeks ago, but I'm having a brain fart, and can't remember how to do it. How do I detect when a certain key is pressed in VB .NET 2002? I know it has something to do with KeyDown, KeyPress, and KeyUp. Basically I want to do "if the down arrow is pushed, do this" or "if the user presses the y key do this".

I know that was worded really badly, but I think it gets my point across (I'm operating on not much sleep).
Rekarp wrote:
mako321 wrote:What makes you head ninja, anyways? :wink:
Cause I am Abe F#!@ing Lincoln. :mrgreen:
ghosstt
Senior Member
Posts: 1551
Joined: Mon Feb 26, 2007 4:14 pm

Post by ghosstt »

well, i thought you could just handle a keypress on a certain button, but you can't. (vb.net) i asked my programming teacher, and he didn't know either. (i wanted to make W move forward more.) though there are defaults for the arrow keys, just open your code page, and go to form and then key UP
Skyone
Moderator
Posts: 6390
Joined: Tue Nov 29, 2005 8:35 pm
Location: it is a mystery
Contact:

Post by Skyone »

ghosstt wrote:well, i thought you could just handle a keypress on a certain button, but you can't. (vb.net) i asked my programming teacher, and he didn't know either. (i wanted to make W move forward more.) though there are defaults for the arrow keys, just open your code page, and go to form and then key UP
...jeez. You need a better teacher.

Look up the Win32 GetAsyncKeyState function.
lifeisbetterwithketchup
Senior Member
Posts: 2180
Joined: Fri Jul 21, 2006 12:08 pm
Steam ID: lifeisbetterwithketchup
Location: Illinois. Whee.

Post by lifeisbetterwithketchup »

How do you utilize that? When I write a subroutine for KeyUp, it just triggers whenever any key is lifted up, not any specific key pushed down. :?

EDIT: Didn't see your post, Sky. I'm looking that up right now.

EDIT 2: Googled GetAsyncKeyState and couldn't find anything relating to VB, just C++.
Rekarp wrote:
mako321 wrote:What makes you head ninja, anyways? :wink:
Cause I am Abe F#!@ing Lincoln. :mrgreen:
Skyone
Moderator
Posts: 6390
Joined: Tue Nov 29, 2005 8:35 pm
Location: it is a mystery
Contact:

Post by Skyone »

Win32 APIs are linked, they can be used with any language that supports linking.

Code: Select all

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

Code: Select all

GetAsyncKeyState(30);
lifeisbetterwithketchup
Senior Member
Posts: 2180
Joined: Fri Jul 21, 2006 12:08 pm
Steam ID: lifeisbetterwithketchup
Location: Illinois. Whee.

Post by lifeisbetterwithketchup »

OK, I tried

Code: Select all

        If GetAsyncKeyState(65) = True Then
            MsgBox("lol")
        End If
and

Code: Select all

        If GetAsyncKeyState(65) Then
            MsgBox("lol")
        End If
and neither worked. :?
Rekarp wrote:
mako321 wrote:What makes you head ninja, anyways? :wink:
Cause I am Abe F#!@ing Lincoln. :mrgreen:
JAY
Posts: 132
Joined: Tue Nov 22, 2005 4:06 pm

Post by JAY »

I did some game programming in Excel with Visual Basic , so the code might be the same as in VB.NET (though I'm not familiar with that software at all). For key presses, I used:

Code: Select all

Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
'Pressing W (up)'
If KeyCode = 87 Then
'etc...'
...where the Keycode variable is set to the ASCII equivalent for the key being pressed. In Excel's Visual Basic, there is no equivalent for the arrow keys (and a few others), so that may change the button setup you were hoping for.
lifeisbetterwithketchup
Senior Member
Posts: 2180
Joined: Fri Jul 21, 2006 12:08 pm
Steam ID: lifeisbetterwithketchup
Location: Illinois. Whee.

Post by lifeisbetterwithketchup »

That's how I ended up doing it. Another guy in my VB class showed me that.
Rekarp wrote:
mako321 wrote:What makes you head ninja, anyways? :wink:
Cause I am Abe F#!@ing Lincoln. :mrgreen:
Post Reply