+ Reply to Thread
Results 1 to 3 of 3

Thread: Keyboard keys in VB.NET

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Keyboard keys in VB.NET

    In this tutorial I'll show you to use the Keyboard key events to get info about when a key is pressed and which key it is. There's 4 different key events KeyDown, KeyPressed, KeyUp and PreviewKeyDown. The first and the third one is working the same while the last one is a very little bit different but the mayor thing with the last one is that it's called first, then the Normal KeyDown and then the KeyPressed Event, if the key isn't released these three events will continue to be called in this order. Then when the key is released the KeyUp event is called. The KeyPressed event has a lot fewer functions then the other three, it have almost nothing but a good thing you can do with it which you can't with the others is to get what text the press should have generated, more to that later.







    Get the Key's Value

    To get the key which was pressed you can use e.KeyCode, e.KeyValue and e.KeyData. KeyCode and KeyData are enums so they are probably easier to use since they key's numbers doesn't follow any easy order. e.KeyCode, e.KeyValue and e.KeyData works for PreviewKeyDown, KeyDown and KeyUp but not for KeyPressed. An example on how you can use it:



    [highlight=VB.NET] Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    Select Case e.KeyCode
    Case Keys.W
    MessageBox.Show("Move forward")
    Case Keys.A
    MessageBox.Show("Move left")
    Case Keys.S
    MessageBox.Show("Move back")
    Case Keys.D
    MessageBox.Show("Move right")
    End Select
    End Sub[/highlight]



    Shift, Control and Alt

    When using the events PreviewKeyDown, KeyDown and KeyUp you can use e.Shift, e.Control and e.Alt to get a boolean value indicating if Shift is down, Control is down and if Alt is down. You can also get the same thing by using My.Computer.Keyboard.ShiftKeyDown, My.Computer.Keyboard.ControlKeyDown and My.Computer.Keyboard.AltKeyDown and this could be used from everywhere not only from inside these three events. An example using the first way:


    [highlight=VB.NET] Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    MessageBox.Show("Alt: " & e.Alt & " Ctr: " & e.Control & " Shift: " & e.Shift)
    End Sub[/highlight]







    Caps Lock, Scroll Lock and Num Lock


    You can also use My.Computer.Keyboard.CapsLock, My.Computer.Keyboard.ScrollLock and My.Computer.Keyboard.NumLock to receive boolean values indicating if the "locks" is enabled or not.




    e.KeyChar

    e.KeyChar can only be used by the KeyPressed Event. It will return the same thing as when you're typing with your keyboard. For example, if you're pressing the a button it will return "a" but if you at the same time has CapsLock enabled or holding shift down it will automatically return "A" instead. So this could be used if you want to allow a user to write somewhere else then a textbox or something similar. The simple example below will use e.KeyChar to allow the user to type in a label:


    [highlight=VB.NET] Private Sub frmMain_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
    lblTest.Text &= e.KeyChar
    End Sub[/highlight]





    SendKeys

    You can also simulate keystrokes. If you use My.Computer.Keyboard.SendKeys("a") for example it would behave as you would have typed a on the keyboard. It's a simulation of the key press rather then that the application think the key was pressed since you can simulate keystrokes couch by other programs, so therefor you can make the program write texts for you An example for this is:


    [highlight=VB.NET] My.Computer.Keyboard.SendKeys("H")
    My.Computer.Keyboard.SendKeys("e")
    My.Computer.Keyboard.SendKeys("l")
    My.Computer.Keyboard.SendKeys("l")
    My.Computer.Keyboard.SendKeys("o")[/highlight]

    You can also do it all in one row:


    [highlight=VB.NET] My.Computer.Keyboard.SendKeys("Hello")[/highlight]



    That was everything for this tutorial, have a fun time coding.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Keyboard keys in VB.NET

    Very handy! Nice work. +rep

  4. #3
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Keyboard keys in VB.NET

    Nice +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Similar Threads

  1. Replies: 2
    Last Post: 01-29-2011, 01:15 PM
  2. disable keys
    By gor in forum Pascal and Delphi
    Replies: 2
    Last Post: 12-10-2009, 04:10 AM
  3. Binding keyboard keys in Linux
    By TcM in forum Linux Tutorials, Guides and Tips
    Replies: 3
    Last Post: 10-11-2009, 11:58 PM
  4. help with foreign keys
    By the_code_charmer in forum Database & Database Programming
    Replies: 11
    Last Post: 12-11-2008, 12:30 PM
  5. SSH Keys
    By C3P0 in forum Linux Networking
    Replies: 2
    Last Post: 05-27-2007, 03:07 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts