CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2001
    Location
    Rochester, NY
    Posts
    28

    how to determine which keystroke?

    I'd like to check which key on the keyboard was pressed. My first attempt looked somthing like this:

    Private Sub Button1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Button1.KeyPress
    If e = System.Windows.Forms.Keys.Left Then

    End If
    End Sub

    This gets a syntax error on the right side of the equal sign. Anyone see what I'm doing wrong?

    Thanks in advance.

  2. #2
    Join Date
    May 2002
    Location
    Toronto
    Posts
    167
    Are you trying to figure out if the button is left clicked or right clicked ? I just set up a blank form and put a button on it and the keypress didn't work for the mouse at all it worked only for the keyboard using your your code. Get rid of the if statement and put a break at the Private Sub ...

    when you debug you will see it only breaks if there is a keypress not a button click

  3. #3
    Join Date
    Aug 2001
    Location
    Rochester, NY
    Posts
    28
    Actually, I'm trying to check if the left arrow was pressed. I'm trying to make the buttons (or any controls) movable. Depending on which control has focus, when an arrow key is pressed the control will be moved in that direction.

  4. #4
    Join Date
    May 2002
    Location
    Toronto
    Posts
    167
    I could be wrong, but wouldn't that be the sender and not e ?

  5. #5
    Join Date
    Aug 2001
    Location
    Rochester, NY
    Posts
    28
    I found that e.KeyChar references the key that was pressed. However, the arrow keys don't trigger the KeyPress event. Instead it changes the focus to the next control in the Tab Stop order.

    Maybe I'll use the arrows on the number pad instead. These seem to trigger the KeyPress event - i.e., 4 = left, 2 = down, etc.

    Thanks for the help Jym!

  6. #6
    Join Date
    Jan 2003
    Location
    Amsterdam, Netherlands
    Posts
    97
    To capture a key press on a empty form.

    Set the KeyPreview property on the form = True

    and implement the following code for your own form.

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    ' Capture Keys
    Select Case e.KeyCode
    Case Keys.Left, Keys.NumPad4
    ' .......

    Case Keys.Right, Keys.NumPad6
    ' .......

    Case Keys.Up, Keys.NumPad8
    ' .......

    Case Keys.Down, Keys.NumPad2
    ' .......

    End Select
    End Sub

    This only works if you don't have any buttons on the form.
    Because the the cursurkeys will switch the focus from button to button and the keydown event isn't raised.

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

Featured