I was wondering, how exactly would I go about doing that.

I want assign the arrow keys to command buttons.

For Example:

Command1 would be the UP arrow key, command2 would be the LEFT arrow key, command3 would be the RIGHT arrow key, command4 would be the DOWN arrow key.

Recommended Answers

hi, have you tried using virtual key codes?

check out this link it might help you.

http://www.rolbe.com/2009/03/25/visual-basic-virtual-key-codes/

:)

Jump to Post

All 2 Replies

In a module the following -

'GetAsyncKeyState uses user32.dll. The only parameter it takes
'is a long "vKey", which is a virtual key. It returns a 16 bit
'integer which describes the position of the key at the time the
'function is called, as well as the last time the function was
'called. Bit 0 indicates the position the key was in since the
'last time the function was called, and will be 1 if the key was
'pressed, 0 if not. Bit 15 will be 1 if the key is currently down,
'0 if it is up.
'*****************************************************************
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

'******************************************************************
'GetActiveWindow is a simple API call that takes no parameters, and
'only returns the hWnd of the active window.
'******************************************************************
Private Declare Function GetActiveWindow Lib "user32" () As Long

'*******************************************************************
'Constants to define virtual keys. Used for the API GetAsyncKeyState
'*******************************************************************
Const VK_RIGHT& = &H27, VK_UP& = &H26, VK_LEFT& = &H25, VK_DOWN& = &H28

Under your command button KeyPress Event. These covers all other keyascii -

Dim sCode As String
Select Case KeyAscii
Case 13 ' Enter
sCode = "Enter"
Case 16 ' Shift
sCode = "Shift"
Case 17 ' Ctrl
sCode = "Ctrl"
Case 18 ' Alt
sCode = "Alt"
Case 37 ' Left
sCode = "Left"
Case 38 ' Up
sCode = "Up"
Case 39 ' Right
sCode = "Right"
Case 40 ' Down
sCode = "Down"
Case 46 ' Del
sCode = "Del"
Case 91 ' Menu
sCode = "Menu"
Case Else ' Other values.
sCode = Chr(KeyAscii)
End Select
Text1.Text = Text1.Text & "(" & sCode & ", " & KeyAscii & ") "

' Select Case KeyCode
' Case 38 ' Up
' Text1.Text = Text1.Text & "Up," & CStr(KeyCode)
' Case 46 ' Del
' Text1.Text = Text1.Text & "Del," & CStr(KeyCode)
' Case Else ' Other values.
' If Text1.Text = "" Then
' Text1.Text = "(" & Chr(KeyAscii) & "," & KeyAscii & ")"
' Else
' Text1.Text = " " & Text1.Text & "(" & Chr(KeyAscii) & "," & KeyAscii & ")"
' End If
' End Select

End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.