Results 1 to 5 of 5

Thread: [2.0] key shortcuts while form is inactive

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    26

    [2.0] key shortcuts while form is inactive

    here's what i would like to know how to do (2005 express edition):
    let's suppose that my form, or application, is not active, minimized, or hidden, in the notification area. i want to be able to press a key, f10 for example, and then have it do a command. that command is not important right now, but let's just suppose that it's to show a message box or something simple.
    thanks to all who help.

  2. #2
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: [2.0] key shortcuts while form is inactive

    If the application is in focus you could use a keyevent handler. For e.g. lets say an application that is displaying advertisements and when the user clicks ESC it stops, this could be used. e.g.

    if (e.KeyValue == 27) // User presses ESC, Key numeration code is 27.
    {
    MessageBox.Show("You just pressed the Escape key!");
    }

    The downside to this is that the application must be in focus. To "listen" for a key when the application is not in focus, you need some sort of key hook and simply read when ESC / F1 is pressed. There's a nice example of this in the Code Project site, probably tomorrow when i have more time I would post a link for you.

    Jennifer.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    26

    Re: [2.0] key shortcuts while form is inactive

    and this will work if the form is inactive, or minimized?

  4. #4
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: [2.0] key shortcuts while form is inactive

    The key hook will work if the form is minimized. I faintly remember that I saw a nice example of it on code project. When i get time later I will do some searching. But in the meantime here's 2 links could be of interest to you:

    http://search.microsoft.com/results....=keyboard+hook
    http://search.microsoft.com/results....-AU&FORM=QBME1


    Jenni

  5. #5
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: [2.0] key shortcuts while form is inactive

    http://www.codeproject.com/csharp/globalhook.asp
    http://weblogs.asp.net/jdanforth/arc...21/454219.aspx
    http://blogs.msdn.com/toub/archive/2...03/589423.aspx

    Just to share, using keyboard hooks is not a good idea unless you have a valid reason for it. Also, try to use shortcuts which are otherwise not system specific.

    For example, your application pop ups a message box on pressing CTRL+G. If your application is inactive and you have Notepad open (active I mean), then pressing CTRL+G could be problematic because it is a shortcut in Notepad also.

    Hope it helps you.
    Show Appreciation. Rate Posts.

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