Hi everyone,,, Can someone tell me, how to use the KeyPress Function? what is the command for a KeyPress function? thank you
KeyPress Function
Collapse
X
-
Dear Friend,
Keypress event will fire when the key is pressed on any control
example on a text box when you type something the keypress event will get fired. the value returns from that is keyascii of the key pressed.
when you press a A in a text box it will return a value 97. Through that we can restrict the user entry values in the text box.
Try this
Private Sub Text1_KeyPress( KeyAscii As Integer)
if keyascii = 97 then
MsgBox "You pressed A"
end if
End Sub
or
Private Sub Text1_KeyPress( KeyAscii As Integer)
If KeyAscii = Asc("A") Then
MsgBox "hi"
End If
End Sub -
Hi....
If you still dont get it...
I have her a simple code to display the equivalent keyascii from the keyboard:
as an example i use the form for demo.
Create new project from VB...
and from the form properties - Keypreview set it to TRUE
Private Sub Form_KeyPress(K eyAscii As Integer)
MsgBox KeyAscii
End Sub
NOTE:
A-Z=65-122
a-z=97-122
0-9=48-57
<ENTER>=13
Esc=27
etc...
just try to run the program and it will display the equivalent keyascii...
GoodLuck....:)Comment
-
Thank you so much for all the ideas. i have already finished my keypress project and for a contributons i will share the code that i build to come up with a correct output..
Private Sub Text1_KeyDown(K eyCode As Integer, Shift As Integer)
Dim strKeyPressed As String
Select Case KeyCode
Case vbKeyControl
strKeyPressed = "Ctrl"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyShift
strKeyPressed = "Shift"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyF1
strKeyPressed = "F1"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyF2
strKeyPressed = "F2"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyF3
strKeyPressed = "F3"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyF4
strKeyPressed = "F4"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyF5
strKeyPressed = "F5"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyF6
strKeyPressed = "F6"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyF7
strKeyPressed = "F7"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyF8
strKeyPressed = "F8"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyF9
strKeyPressed = "F9"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyF10
strKeyPressed = "F10"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyF11
strKeyPressed = "F11"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyF12
strKeyPressed = "F12"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyCapital
strKeyPressed = "Caps Lock"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyEscape
strKeyPressed = "Esc"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyTab
strKeyPressed = "Tab"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeySpace
strKeyPressed = "Space"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyBack
strKeyPressed = "Back Space"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyPrint
strKeyPressed = "Print Screen"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyScroll
strKeyPressed = "Print Screen"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyPause
strKeyPressed = "Pause"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyInsert
strKeyPressed = "Insert"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyHome
strKeyPressed = "Home"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyPageUp
strKeyPressed = "Page Up"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyPageDown
strKeyPressed = "Page Down"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyDelete
strKeyPressed = "Delete"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyEnd
strKeyPressed = "End"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyUp
strKeyPressed = "Arrow Up"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyDown
strKeyPressed = "Arrow Down"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyLeft
strKeyPressed = "Arrow Left"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyRight
strKeyPressed = "Arrow Right"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyNumlock
strKeyPressed = "Num Lock"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
End Select
End Sub
Private Sub Text1_KeyPress( KeyAscii As Integer)
Dim strKeyPressed As String
Dim strAscii As String
Dim Enter As String
Dim Esc As String
Dim Tb As String
Dim Space As String
Dim BackSpace As String
strKeyPressed = Chr(KeyAscii)
strAscii = CStr(KeyAscii)
Enter = "Enter"
Esc = "Esc"
Tb = "Tab"
Space = "Space Bar "
BackSpace = "Back Space"
Label1.Caption = "Character Pressed : " & strKeyPressed
Text1.Text = ""
If strAscii = 13 Then
Label1.Caption = "Character Pressed : " & Enter
Text1.Text = Enter
End If
If strAscii = 27 Then
Label1.Caption = "Character Pressed : " & Esc
Text1.Text = Esc
End If
If strAscii = 9 Then
Label1.Caption = "Character Pressed : " & Tb
Text1.Text = Tb
End If
If strAscii = 32 Then
Label1.Caption = "Character Pressed : " & Space
Text1.Text = Space
End If
If strAscii = 8 Then
Label1.Caption = "Character Pressed : " & BackSpace
Text1.Text = BackSpace
End If
End SubComment
-
Originally posted by pa2ratcziThank you so much for all the ideas. i have already finished my keypress project and for a contributons i will share the code that i build to come up with a correct output..
Private Sub Text1_KeyDown(K eyCode As Integer, Shift As Integer)
Dim strKeyPressed As String
Select Case KeyCode
Case vbKeyControl
strKeyPressed = "Ctrl"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyShift
strKeyPressed = "Shift"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyF1
strKeyPressed = "F1"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyF2
strKeyPressed = "F2"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyF3
strKeyPressed = "F3"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyF4
strKeyPressed = "F4"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyF5
strKeyPressed = "F5"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyF6
strKeyPressed = "F6"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyF7
strKeyPressed = "F7"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyF8
strKeyPressed = "F8"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyF9
strKeyPressed = "F9"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyF10
strKeyPressed = "F10"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyF11
strKeyPressed = "F11"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyF12
strKeyPressed = "F12"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyCapital
strKeyPressed = "Caps Lock"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyEscape
strKeyPressed = "Esc"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyTab
strKeyPressed = "Tab"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeySpace
strKeyPressed = "Space"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyBack
strKeyPressed = "Back Space"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyPrint
strKeyPressed = "Print Screen"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyScroll
strKeyPressed = "Print Screen"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyPause
strKeyPressed = "Pause"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyInsert
strKeyPressed = "Insert"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyHome
strKeyPressed = "Home"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyPageUp
strKeyPressed = "Page Up"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyPageDown
strKeyPressed = "Page Down"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyDelete
strKeyPressed = "Delete"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyEnd
strKeyPressed = "End"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyUp
strKeyPressed = "Arrow Up"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyDown
strKeyPressed = "Arrow Down"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyLeft
strKeyPressed = "Arrow Left"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyRight
strKeyPressed = "Arrow Right"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
Case vbKeyNumlock
strKeyPressed = "Num Lock"
Label1.Caption = " Character Pressed: " & strKeyPressed
Text1.Text = strKeyPressed
End Select
End Sub
Private Sub Text1_KeyPress( KeyAscii As Integer)
Dim strKeyPressed As String
Dim strAscii As String
Dim Enter As String
Dim Esc As String
Dim Tb As String
Dim Space As String
Dim BackSpace As String
strKeyPressed = Chr(KeyAscii)
strAscii = CStr(KeyAscii)
Enter = "Enter"
Esc = "Esc"
Tb = "Tab"
Space = "Space Bar "
BackSpace = "Back Space"
Label1.Caption = "Character Pressed : " & strKeyPressed
Text1.Text = ""
If strAscii = 13 Then
Label1.Caption = "Character Pressed : " & Enter
Text1.Text = Enter
End If
If strAscii = 27 Then
Label1.Caption = "Character Pressed : " & Esc
Text1.Text = Esc
End If
If strAscii = 9 Then
Label1.Caption = "Character Pressed : " & Tb
Text1.Text = Tb
End If
If strAscii = 32 Then
Label1.Caption = "Character Pressed : " & Space
Text1.Text = Space
End If
If strAscii = 8 Then
Label1.Caption = "Character Pressed : " & BackSpace
Text1.Text = BackSpace
End If
End Sub
we can use chr(KeyAscii) or chr(KeyCode). no need to write above Program to get Key CharacterComment
-
There is bug in ms access when use used Downkey in your form it cause to Numlock error i mean during data entry numlock automaticlly down here is the prefect soultion
Option Compare Database
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Declare Function GetKeyboardStat e Lib "user32" (pbKeyState As Byte) As Long
Private Declare Function SetKeyboardStat e Lib "user32" (lppbKeyState As Byte) As Long
Function GetNumlock() As Boolean
' Return or set the Numlock toggle.
GetNumlock = CBool(GetKeySta te(vbKeyNumlock ) And 1)
End Function
Sub SetNumlock(Valu e As Boolean)
' Return or set the Numlock toggle.
Call SetKeyState(vbK eyNumlock, Value)
End Sub
Private Sub SetKeyState(int Key As Integer, fTurnOn As Boolean)
Dim abytBuffer(0 To 255) As Byte
GetKeyboardStat e abytBuffer(0)
abytBuffer(intK ey) = CByte(Abs(fTurn On))
SetKeyboardStat e abytBuffer(0)
End Sub
for further info
shahzad arain
92-3345307738
shahzad.cdcu@gm ail.comComment
Comment