Results 1 to 8 of 8

Thread: [RESOLVED] [2005] keyboard events

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Resolved [RESOLVED] [2005] keyboard events

    Hello, i am not a begginer programmer but i never have the need to use keyboard events and when i learned a bit after i forgot how. I want to make a if statement that does this: When the user click left arrow key move picture box to the left. I basicly only need the code for the if left arrow key is pressed. I do not want to use a case i know there is another way besides using select case, since when i learned i didn't use that method.

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] keyboard events

    You need to handle the appropriate controls KeyDown event, I assume you'll be needing the Forms KeyDown event:

    vb Code:
    1. Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    2.         If e.KeyCode = Keys.Left Then
    3.             'Do whatever
    4.         End If
    5.     End Sub
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: [2005] keyboard events

    Thankyou that is the Exact code i wanted thankyou very much because whenever i asked someone else they always used select cases and i dont get the concept of them.

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] keyboard events

    Using Select Case provides an advantage of readability versus having many If-statements. See here:

    vb Code:
    1. Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    2.         If e.KeyCode = Keys.Left Then
    3.             'Do whatever
    4.         ElseIf e.KeyCode = Keys.Right
    5.             'Do something else
    6.         ElseIf e.KeyCode = Keys.Up
    7.             'Do something crazy
    8.         End If
    9.     End Sub

    The equivallent Select Case would be:

    vb Code:
    1. Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    2.         Select Case e.KeyCode
    3.             Case Keys.Left
    4.                 'Do whatever
    5.             Case Keys.Right
    6.                 'Do something else
    7.             Case Keys.Up
    8.                 'Do something crazy
    9.         End Select
    10.     End Sub
    They do the exact same thing, but the latter results in less code and is easier to read.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: [2005] keyboard events

    oh whell then thankyou again i think i am going to use a select case, but where u wrote select case e.keycode then you jsut wrote case keys.left case keys.right where in if statements u have to use e.keycode = keys.left etc.. so where u wrote in select case e.keycode that determines???

  6. #6
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] keyboard events

    Quote Originally Posted by noahssite
    oh whell then thankyou again i think i am going to use a select case, but where u wrote select case e.keycode then you jsut wrote case keys.left case keys.right where in if statements u have to use e.keycode = keys.left etc.. so where u wrote in select case e.keycode that determines???
    http://msdn2.microsoft.com/en-us/lib...hx(VS.71).aspx
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: [2005] keyboard events

    I understand now thankyou...

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: [RESOLVED] [2005] keyboard events

    This Is Resolved!!!!!

Posting Permissions

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



Click Here to Expand Forum to Full Width