CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2001
    Location
    Salzburg, Austria
    Posts
    126

    simulating hotkey

    Hi!
    How can I detect that a key has been pressed (F5 f.e.) just in my application? I try the MCL Hotkey control from http://www.MerrionComputing.com but affectes the rest of the application (Other applications that use these keys don´t react anymore when my application runs minimized).


  2. #2
    Join Date
    Oct 2001
    Location
    Phoenix, AZ
    Posts
    54

    Re: simulating hotkey

    You have to enable KeyPreview on the form you wish to detect the keyup or keydown.


    'for instance,
    private Sub Form_Load()
    Form1.KeyPreview = true
    End Sub



    Then, trap the event in your form's KeyUp/KeyDown:

    private Sub Form_KeyUp(KeyCode as Integer, Shift as Integer)
    If KeyCode = vbKeyF5 then
    MsgBox "F5 has been pressed!"
    End If
    End Sub




    Software is like sex, it's better when it's free - Linus Torvalds
    Software is like sex, it's better when I get paid for it. - me

  3. #3
    Join Date
    Jul 2001
    Location
    Salzburg, Austria
    Posts
    126

    Re: simulating hotkey

    Thanks!
    It works fine, but my problem is that I have a MDI and a few amount of Child forms, and I cannot apply your hint for the Main Form, just for the child ones, which means more code to maintain. I was thinking more just placing some kind of control on the MDI form.
    Jaime.


  4. #4
    Join Date
    Oct 2001
    Location
    Phoenix, AZ
    Posts
    54

    Re: simulating hotkey

    Is F5 tied to some sort of function in your app?

    Software is like sex, it's better when it's free - Linus Torvalds
    Software is like sex, it's better when I get paid for it. - me

  5. #5
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: simulating hotkey

    You could call the MCL Hotkey's Unregister method when your application loses focus and call the Register method when it gains the focus...but that is a bit overkill as that control is intended for system-wide hotkeys.

    Other than that you would need to subclass each of your forms (you only need one VB_WNDPROC) and whenever you get a WM_CHAR command check if it was F5 and if so process it, otherwise pass it on for default processing.

    HTH,
    Duncan

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    Check out the new downloads - ImageMap.ocx is the VB control that emulates an HTML image map, EventVB.OCX for adding new events to your VB form and adding System Tray support simply, MCL Hotkey for implemenmting system-wide hotkeys in your application...all with source code included.
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  6. #6
    Join Date
    Jul 2001
    Location
    Salzburg, Austria
    Posts
    126

    Re: simulating hotkey

    My application has to run at the same time that other applications that use these keys (like AS400 emulation). I thought I could use these keys because the users are used to work with them and it is more confortable for them when I keep something from the "old" application. So they should be able to use these keys along the whole application. But I think I will use your first suggestion (in fact there are not so many child forms and in future this will not change, the number of forms).
    Jaime


  7. #7
    Join Date
    Oct 2001
    Location
    Phoenix, AZ
    Posts
    54

    Re: simulating hotkey

    What I was going to suggest is creating a menu on your MDI form that has a binding to F5.

    Software is like sex, it's better when it's free - Linus Torvalds
    Software is like sex, it's better when I get paid for it. - me

  8. #8
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: simulating hotkey

    The new release of the MCL Hotkey control has an enabled property so you can turn the hotkey on and off when your app is minimised/maximised.

    HTH,
    Duncan

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com/EventVB/Overview.htm - Flatten the API learning curve
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

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