Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Working with Key Board Events in VB.net Application.

Author: DevASP
Download Source Code : 338_Keyboard_controls.zip

In this simple article you will learn to work with Key Board events when you press key down when you leave key.

Create a New Project in VB.net. Drag Three Button on the form1. Write code in key down event of form1

 

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

 

   MsgBox("key down evetn of form " & e.KeyValue & " = " & e.KeyData.ToString)

 

End Sub

But if you want to check button pressed with Shift Key then with this event you cant do this. For this purpose use Key Up event. Write code in key Up event of form1

Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp

 

     MsgBox("key Up evetn of form " & e.KeyValue & " = " & e.KeyData.ToString)

 

End Sub

  • Select button1 KeyDown event and write following code in this:

Private Sub Button1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Button1.KeyDown

 

 MsgBox("key down evetn of button1 KEY VALUE=" & e.KeyValue & " = " & e.KeyData.ToString)

 

End Sub

  • Select button2 KeyUp event and write following code in this

Private Sub Button2_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Button2.KeyUp

 

 MsgBox("key Up evetn of button2 KEY VALUE=" & e.KeyValue & " = " & e.KeyData.ToString)

 

End Sub

  • Select button3 KeyPress event and write following code in this:

Private Sub Button3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Button3.KeyPress

 

        MsgBox("key Up evetn of button3 KEY VALUE=" & e.KeyChar)

 

End Sub

  • With these three events you can work with key board input easily.
Article Comments
Respected,
thats so good knowledge about the key board events so send me more information about that

Posted on 2/20/2007 5:51:17 AM by adnan

I have a grid on the form. When my focus is on Grid cell, then my form_keydown event dont fires. I has set the KeyPreview property to TRUE, but that still dont work....I m trying to TRAP F12 function key.....i want it to fire Keydown under any circumstances.Please help

Posted on 7/9/2007 11:08:12 AM by Ambresh Gaur

It is good but i want separate keywods and events in vb.net with some one to two line discription.
ThankYou

Posted on 8/23/2007 3:44:53 AM by Vikash Chandra Sinha

Really nice article. It makes the idea very clear.

Posted on 10/23/2007 5:44:15 PM by bhawaniprasad

i want to move cursor next---hit the enter key again & again pls give me code

Posted on 1/4/2008 2:22:32 AM by jitu

Hello,

With regard to Keydown/Keypress event i have one question here. I have a webbrowser control which displays the browser opened. I need to capture the value entered by the user in the fields present in that browser. For Eg: If there is a textbox in a browser i need to get the data entered by user. I could find only previewkeydown event associated with webbrowser but it gets me the value twice.

Please help to get rid of this problem.

Thanks
Vinod

Posted on 3/25/2008 1:08:54 PM by vinod

well, i still not able to get idea. may be u not show the namespaces required for these events to handle...

Posted on 4/21/2008 5:49:32 AM by fakhre

Its very good Code.

I want to check the key stroks of whole keyboard.
I mean when i press any key from keyboard then show the key on vb form by using vb.net.
Please help me as soon as possible.

Posted on 8/28/2008 5:42:12 AM by Aamir Rahim Yar khan

My real question is that when i press any key form keyboard for example i press A or any other key (A to Z, 0 to 9) then these all keys mean A to Z, 0 to 9 will show on vb.net's form.
after that i will store these keys in Access database and retreive it.

Please help me

Posted on 9/1/2008 2:24:15 AM by Aamir

hi dear
im alireza from iran
your information is usefull
thanks so much !
by

Posted on 12/7/2009 11:28:38 AM by alireza

sir, in keypress event only accept 0-9 & single time "." in a textbox
pl send me soluction.

Posted on 12/17/2009 8:34:55 PM by sanjay jindal

great work!

Posted on 1/22/2010 8:41:48 AM by baskar

hey i was using a key bordfuture "text_keypress" what i need know is there is Beep noise whenever a key is click .if is there any way to get rid of this noise.

tku for helping me

Posted on 1/26/2010 5:53:04 AM by kokob

hi... i want coding for key press event.. if i press the keys in alpha numeric keyboard that has to print key values

Posted on 2/6/2010 2:16:57 AM by Lakshmitha

Hello,

With regard to Keydown/Keypress event i have one question here. I have a webbrowser control which displays the browser opened. I need to capture the value entered by the user in the fields present in that browser. For Eg: If there is a textbox in a browser i need to get the data entered by user. I could find only previewkeydown event associated with webbrowser but it gets me the value twice.

Please help to get rid of this problem.

Thanks
Mehul Kahodariya

Posted on 4/8/2010 12:07:07 PM by mehul kahodariya

Hello, this information was really useful. But...
I would like to know if you can help me with my project. I'm creating a 'Jeopardy game'. I have created almost 90% of the game. However, I got stuck at one part. I'm trying to make a buzzer using the ascii values. I'm using a clickhandler and I would like to have this ascii code under the clickhandler. When I click on a button, an answer comes up. I have to be able to have a buzzer choosing what player would ask the question. So far, I can select the players before the answer comes up. However, I do not want that. I need to select the player after the msgbox with the answer pops up. Can anyone please help me with this small problem??? Thanks in advance.

Posted on 6/3/2010 10:52:05 AM by Altair Da Silva

that was really a good example. but i want something else. can any one please guide me.
i have two buttons on a form say button1 and button2. when i press enter key on button1 i want the focus should go to button2 but what it is doing is when i press enter on button1 then it is firing the button1_click event. i have tried in button1_keydown, button1_keypress events but it is of no useful.
can please any guide in.
Thanks in advance.

Posted on 8/11/2010 8:18:41 AM by Jagdish

hi!
i wana interrupt any key outsize form, how to?

Posted on 8/20/2010 8:06:20 PM by gnkien

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Working with Mouse Events in VB.Net Application

Disclaimer - Privacy
© 2002-2017 DevASP.net