#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    38
    Rep Power
    15

    Smile How to detect arrow keys in vb.net?


    Hi,

    How do we detect arrow keys(up,down,left,right) in a vb.net application? Also, what are the code values for these keys?

    The application consists of a form that has an image control on it. When the user presses the right or left arrow keys, the image is supposed to move to the right or left.
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2004
    Location
    Hampshire, UK
    Posts
    989
    Rep Power
    319
    override the ProcessDialogKey function of the form... use the keyData argument and check for Keys.Left, Keys.Right, Keys.Up or Keys.Down and move the location property of your image accordingly

    hope this helps

    Comments on this post

    • Randolpho agrees
    Scott Perham, BSc (hons), CCNA, MCPD
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Location
    Middle TN
    Posts
    504
    Rep Power
    23
    I agree.
    Last edited by Randolpho; April 21st, 2006 at 12:35 AM.
    Bad code monkey! No banana!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Location
    Noida/India
    Posts
    127
    Rep Power
    14
    hi
    may i know how to override the ProcessDialogKey function of the form
    I can't find any function of form like ProcessDialogKey

    Thanks
    Hnadshakeit
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2004
    Location
    Hampshire, UK
    Posts
    989
    Rep Power
    319
    ok.. in vb there are a couple of dropdowns at the top of the code page... the left one has form overrides in it... when you select that, the right dropdown is populated with all the overrides for the form... processdialogkey is in there.

    There is an easier way... use c# :P
    Scott Perham, BSc (hons), CCNA, MCPD
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Location
    Noida/India
    Posts
    127
    Rep Power
    14
    hi scoper,

    i am trying in C# not in vb
    how to find there??
  12. #7
  13. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Location
    Middle TN
    Posts
    504
    Rep Power
    23
    Why do you need to "find" it? Just type it in.

    C# Code:
    protected override bool ProcessDialogKey (Keys keyData)
    {
      // your code. 
    }


    Don't forget to return true if you want the key to be considered "processed", i.e. not passed on to other key event handlers.

    Check out the following for more details:
    http://msdn2.microsoft.com/en-US/lib...ey(VS.80).aspx
    Bad code monkey! No banana!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    38
    Rep Power
    15
    Thanks scoper........it works
  16. #9
  17. Business Analyst
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2004
    Location
    The 'Ville
    Posts
    1,087
    Rep Power
    27
    I'm having trouble determining where to capture the event.

    I have a form, with a tab control, and on tab "A" I have a panel, which is really the control I want to capture an arrow key press on.

    When the form is first loaded tab "A" has focus. I know that can manually be changed in the code, but I think that's irrelevant because the user could change the focus at any time. I have set the form property KeyPreview to true, which I believe allows the form to capture a keypress event in any control within the form.

    The panel I have created is actually a custom class that inherits the Panel class.

    So my question is, if I want to capture a left arrow key press, what level should I do it at? The left key will not have any significance or different meaning at any other level, so if I need to capture it on the form level, that's fine, but I would like to be able to capture it in this customized Panel class. What are the requirements or required preceding events that would fire the event on the Panel level? Focus on the Panel? Click of the Panel? Nothing?

    Thanks!

    Comments on this post

    • LyonHaert disagrees : With over 700 posts over 4 years, you should know better. Your problem is different. Start a new thread.
    Discontent is the first necessity of progress. - Edison
  18. #10
  19. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2004
    Location
    Hampshire, UK
    Posts
    989
    Rep Power
    319
    I don't think you fully understand your own requirement

    You have overriden a Panel... why would you handle event when you have a plethora of protected methods play with!
    Scott Perham, BSc (hons), CCNA, MCPD
  20. #11
  21. Business Analyst
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2004
    Location
    The 'Ville
    Posts
    1,087
    Rep Power
    27
    Same idea. So that's what I meant... override methods.

    So at what level should that be done? Form? Tab? Panel?
    Discontent is the first necessity of progress. - Edison
  22. #12
  23. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2004
    Location
    Hampshire, UK
    Posts
    989
    Rep Power
    319
    That entirely depends on the context it's going to be used... no one can really answer that question for you - any suggestion would be based on assumptions or creating the most generic component - either way you are much better off working through your requirements and making a decision based on the scenario. Handling your key events on the panel would make your custom panel more generic... but then how much code will you have to write upon capturing custom events and such.
    Scott Perham, BSc (hons), CCNA, MCPD
  24. #13
  25. Business Analyst
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2004
    Location
    The 'Ville
    Posts
    1,087
    Rep Power
    27
    I don't think you completely read my first update. I'll figure it out. Thanks
    Discontent is the first necessity of progress. - Edison

IMN logo majestic logo threadwatch logo seochat tools logo