Results 1 to 5 of 5

Thread: [RESOLVED] Handling all KeyDowns?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Resolved [RESOLVED] Handling all KeyDowns?

    Hi all,
    is there a way to listen for keydown events on all controls at the same time? I am capturing a barcode scanner's input and the scanner is hooked to a pc. On barcode lock, it simply pastes the number. This means that whatever control currently has the focus, gets also the number.

    VB Code:
    1. Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    2.  
    3.         'MessageBox.Show(e.KeyValue.ToString) helped me to get the keycode
    4.         txtBarcode.Focus()
    5.  
    6.     End Sub
    This works but only if the form itself has the focus. Do I have to check the keydown event of every control or is there an event that fires each time, regardless of where the focus is.

    I guess I could use ZaNi's idea but would it be at all sane with 50+ controls?
    VB 2005, Win Xp Pro sp2

  2. #2
    Fanatic Member BillBoeBaggins's Avatar
    Join Date
    Jan 2003
    Location
    in your database, dropping your tables.
    Posts
    628

    Re: Handling all KeyDowns?

    Create a general Key_Down Event and attach it to all your controls upon creations? Do you manually associate events in VB.Net like in C#?
    Use API and track all window messages, catch the keys your looking for, figure out the handle of the window that triggered the event.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Handling all KeyDowns?

    If you've set the form's KeyPreview property to True then it will receive keyboard events first regardless of which control has focus.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Fanatic Member BillBoeBaggins's Avatar
    Join Date
    Jan 2003
    Location
    in your database, dropping your tables.
    Posts
    628

    Re: Handling all KeyDowns?

    I like the above post, much simpler

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: Handling all KeyDowns?

    Jup, jmcilhinney is great. Still, I will have to investigate your suggestion as well, since the Compact Framework does not offer the KeyPreview property and I have to take care of that on mobile devices too.
    VB 2005, Win Xp Pro sp2

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