detecting keypresses in vb.net
Moderator: Moderators
-
lifeisbetterwithketchup
- Senior Member
- Posts: 2180
- Joined: Fri Jul 21, 2006 12:08 pm
- Steam ID: lifeisbetterwithketchup
- Location: Illinois. Whee.
detecting keypresses in vb.net
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).
I know that was worded really badly, but I think it gets my point across (I'm operating on not much sleep).
Rekarp wrote:Cause I am Abe F#!@ing Lincoln.mako321 wrote:What makes you head ninja, anyways?
...jeez. You need a better teacher.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
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.
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++.
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:Cause I am Abe F#!@ing Lincoln.mako321 wrote:What makes you head ninja, anyways?
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 IntegerCode: Select all
GetAsyncKeyState(30);-
lifeisbetterwithketchup
- Senior Member
- Posts: 2180
- Joined: Fri Jul 21, 2006 12:08 pm
- Steam ID: lifeisbetterwithketchup
- Location: Illinois. Whee.
OK, I tried and and neither worked. 
Code: Select all
If GetAsyncKeyState(65) = True Then
MsgBox("lol")
End IfCode: Select all
If GetAsyncKeyState(65) Then
MsgBox("lol")
End IfRekarp wrote:Cause I am Abe F#!@ing Lincoln.mako321 wrote:What makes you head ninja, anyways?
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:
...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.
Code: Select all
Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
'Pressing W (up)'
If KeyCode = 87 Then
'etc...'-
lifeisbetterwithketchup
- Senior Member
- Posts: 2180
- Joined: Fri Jul 21, 2006 12:08 pm
- Steam ID: lifeisbetterwithketchup
- Location: Illinois. Whee.
