473,396 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

KeyPress Function

Hi everyone,,, Can someone tell me, how to use the KeyPress Function? what is the command for a KeyPress function? thank you
Mar 7 '07 #1
5 58920
samycbe
83
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
Mar 7 '07 #2
Esmael
58
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(KeyAscii 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....:)
Mar 7 '07 #3
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(KeyCode 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
Mar 8 '07 #4
hariharanmca
1,977 1GB
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(KeyCode 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 Character
Mar 8 '07 #5
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 GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
Private Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long


Function GetNumlock() As Boolean
' Return or set the Numlock toggle.
GetNumlock = CBool(GetKeyState(vbKeyNumlock) And 1)
End Function




Sub SetNumlock(Value As Boolean)
' Return or set the Numlock toggle.
Call SetKeyState(vbKeyNumlock, Value)
End Sub

Private Sub SetKeyState(intKey As Integer, fTurnOn As Boolean)
Dim abytBuffer(0 To 255) As Byte
GetKeyboardState abytBuffer(0)
abytBuffer(intKey) = CByte(Abs(fTurnOn))
SetKeyboardState abytBuffer(0)
End Sub

for further info
shahzad arain
92-3345307738
shahzad.cdcu@gmail.com
Apr 2 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Thomas | last post by:
Hey y'all, I'm using firebird 0.7 and developed some kind of inline editing with java script. Therefore I used the keypress event listener. The problem is that I can only enter one character...
4
by: phil_gg04 | last post by:
Dear Javascript Experts, I'm currently implementing Anyterm, a terminal emulator on a web page. It consists of an Apache module, some XmlHTTP and a bit of Javascript. The idea is to give you...
3
by: Raul M. Colon | last post by:
Is possible to assign a click event to a button control in a Web form just pressing the return key? Something like windows forms where you can assign this action to a default control. For example,...
4
by: Tom | last post by:
I have a VB.NET user control that I wrote - this control has three or four other controls on it (textbox, combobox, datetime picker, etc). Now, whenever the control is on a form and the user enters...
1
by: Agnes | last post by:
I design my own datagrid which can turn Enter into 'Tab', BUT I cannot detect the user keypress 'F3' , Why I try to detect keyCode, it only effect on 'keydown' , 'keyup' , but it cannot get my...
3
by: Fia | last post by:
Hi In Visual Basic 6 I could call keypress with an integer of a choosen key. For example I could call a textbox's keypress with the number 13 (for the enter key). But now in Visual Basic .Net I...
5
by: Henry Jones | last post by:
I am new to C# and wanted to capture the KeyPress for a textbox. I created some code as follows: private void textBox3_KeyPress(object sender, System.EventArgs e) { this.textBox2.Text =...
2
by: chatelain | last post by:
Ok a couple things I've got going: On the DataGrid1_CurrentCellChanged I add a button control to the datagridtextbox. This occurs when a mouse click happens inside the cell or the cursor enters the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.