VB .NET , Find window, listen for keys


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: VB .NET , Find window, listen for keys

  1. #1
    Join Date
    Jan 2005
    Posts
    24

    VB .NET , Find window, listen for keys

    I would like to create a program that finds the specified window, and listens for a few hotkeys such as Home.


    Example:

    i open my app, it waits until the target window is found, lets say calculator.
    now that it has found calculator, it will listen for about 4-5 keypress events, such as if i pushed Home to exit calculator, or pushed Insert to clear the screen.The only window i want in focus is the target window, aka calculator.



    Thanks
    I'm here to learn :S

  2. #2
    Join Date
    Jan 2005
    Posts
    24
    actually, it would be awesome if you guys could help me out with just detecting if certain keys are pushed, with out the app being focused or visible.
    I'm here to learn :S

  3. #3
    Join Date
    Jul 2004
    Posts
    13
    any particular language?

  4. #4
    Join Date
    Jul 2004
    Posts
    13
    I'll assume c# - let me know if this is a problem.

    Firstly you need register an event hander. Best place to do this is in the constructor after the call to InitializeComponent <<< VITAL.

    this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);

    Note: For some reason a space is creeping into the above line of code, the bit in brackets should read (this.Form1_KeyPress)

    Then create the method to handle the event

    private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) {
    MessageBox.Show("You pressed " + e.KeyChar.ToString());
    }

    In this case it will just display a MessageBox showing the key that was pressed.

    You could of course do something like this to test for several possibilities.

    private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) {
    switch (e.KeyChar) {
    case Keys.Escape:
    // Do Something
    break;
    case Keys.F1:
    // Do something else
    break;
    }
    }

    In each case the KeyPressEventArgs (e in this case), provides details on the event that was raised.

    Hope this helps.

    Simon
    Last edited by SimonRigby; 04-13-2005 at 06:08 PM.
    I designed, I coded, I raised exceptions

  5. #5
    Join Date
    Jan 2005
    Posts
    24
    thanks alot, great help, if you could help me do this in vb aswell, that would be awesome, thanks.
    I'm here to learn :S

  6. #6
    Join Date
    Jul 2004
    Posts
    13
    No problem,

    Concepts are the same, sytax slightly different.

    Add this to the forms constructor (New method)

    AddHandler Me.KeyPress, AddressOf Form1_KeyPress
    (Note again, this must be AFTER the call to InitializeComponent that VB provides for you.)

    The above line simply says "add an event handler that watches for a KeyPress event on the current form and then run the method stated after the AddressOf statement" - in this case Form1_KeyPress

    The method could be something like this

    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
    MessageBox.Show("You pressed " & e.KeyChar.ToString)
    End Sub

    Hope this helps

    Simon
    I designed, I coded, I raised exceptions

  7. #7
    Join Date
    Jan 2005
    Posts
    24
    hmm i tried to

    Code:
      Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
            AddHandler Me.KeyPress, AddressOf Form1_KeyPress
            MessageBox.Show("You pressed " & e.KeyChar.ToString)
        End Sub
    and i couldnt get it to display the message whenever a key was pressed.

    Also, will this work when the form does not have focus or is even not visible?
    i tried to do stuff with Lib "user32" but just couldnt get it right.

    One last thing, i just need it to listen for certain keys, the keys i had in mind were:

    Insert
    Delete
    Home
    End

    This app is for my friend, the basic idea is, when he has Warcraft III open, he pushes one of the above keys, and it does a funtion. But i just cant figure out how to get my app to listen for keys when the app doesnt have focus and isnt visible, i heard you could use the Msg funtion, but i couldnt get it right either.

    once again thanks for your help
    Last edited by discreet; 04-14-2005 at 04:14 PM.
    I'm here to learn :S

  8. #8
    Join Date
    Jan 2005
    Posts
    24
    ahah found the error for the above code, BUT i still cant get it to listen for a keypress when it has no focus or is not visible.

    The error was:
    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles MyBase.KeyPress

    didnt add the Handles MyBase.KeyPress
    I'm here to learn :S

  9. #9
    Join Date
    Jul 2004
    Posts
    13
    Yeah not having the focus is going to be a problem. I'm afraid your over my head there. Hope someone here can help.

    One thing about the error you found. Part of the reason is wasn't working is, the addhandler needs to be in the constructor, not the method to handle the event.

    Warcraft hey??? I used to play Orcs and Humans many moons ago. Is it related??

    Good luck

    oh and ... yes all of those keys are available in the Keys enumeration. Just change your test to Keys.Insert, Keys.Home etc.

    Cheers
    Last edited by SimonRigby; 04-14-2005 at 05:52 PM.
    I designed, I coded, I raised exceptions

  10. #10
    Join Date
    Apr 2005
    Location
    Dublin, Ireland
    Posts
    3
    To handle key strokes that are being sent to another application you will need to install a keyboard hook.
    From .NET you could do a WH_JORUNALRECORD hook or a WH_KEYBOARD_LL hook. This is not going to be easy - have a search to see if someone has already done this....I have the code in VB6 but have not got around to converting it to .Net yet.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center