none
Global KeyBoard Event Handler RRS feed

  • Question

  • Hi I'm Palm Tree I use C++

    I have a windows forms application that is supposed to have a working keyboard_down event handler. This event handler is triggered when a key is pressed on the while the form has focus. But I want it so that it will trigger whenever any key is pressed in the whole system. What I have tried is Form1.KeyPreview = true but, I want it the event handler to trigger even if my application has lost focus. Ths is because my program becomes invisible at some point and the user isn't going to use the application for some time. When the F5 key is pressed I want to be able to make the form visible again.

    If I can't use an Event Handler for my requirements then what is an adequate alternative
    Monday, July 6, 2009 5:38 AM

Answers

  • I don' think capturing f5 key is a good idea.

    Instead, you can have your application running as a notify icon. And you can assign shortcut keys which will bring up your application to front.
    Thanks,
    A.m.a.L
    .Net Goodies
    Remember to click "mark as answered" when you get a correct reply to your question
    • Marked as answer by Palm.Tree Monday, July 6, 2009 8:20 AM
    Monday, July 6, 2009 5:46 AM

All replies

  • I don' think capturing f5 key is a good idea.

    Instead, you can have your application running as a notify icon. And you can assign shortcut keys which will bring up your application to front.
    Thanks,
    A.m.a.L
    .Net Goodies
    Remember to click "mark as answered" when you get a correct reply to your question
    • Marked as answer by Palm.Tree Monday, July 6, 2009 8:20 AM
    Monday, July 6, 2009 5:46 AM
  • You'll want to use a hot key.  Check this thread for code, there's a C# version further down the thread.  Translating it to C++/CLI shouldn't be too hard.

    Hans Passant.
    • Marked as answer by Palm.Tree Monday, July 6, 2009 10:15 AM
    • Unmarked as answer by Palm.Tree Monday, July 6, 2009 11:10 AM
    Monday, July 6, 2009 6:32 AM
    Moderator
  • Thanks for your help. Yeah I won't use the F5 key I was just thinking off the top of my head!
    • Edited by Palm.Tree Monday, July 6, 2009 8:24 AM Typo
    Monday, July 6, 2009 8:23 AM
  • Dear nobugz

    I converted the C# code in the located thread t C++

    This is what the RegisterHotKey Function looks like:

    private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
    ::RegisterHotKey((HWND) this->Handle.ToPointer(),0, MOD_CONTROL + MOD_SHIFT,(UINT) Keys::H);

     // My code
    PrepareConnection();
    this->FocusTime->Enabled = true;
    this->FocusTime->Start();
    }

    Hectic hey!
    But the problem is when I debug my code I get the following exception at the above line of code:

    " An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.Windows.Forms.dll

    Additional information: Error creating window handle. "

    I really don't have a clue what's going on here because handles and stuff isn't my area

    I'd really appreciate if you could diagnose my problem

    thanks
    Monday, July 6, 2009 11:19 AM
  • I can't debug your code from the info you provided.  The Handle is already created by the time the Load event runs.  Post your project to a file sharing service if you want somebody to take a look at it.  Like skydrive.live.com

    Hans Passant.
    Monday, July 6, 2009 1:08 PM
    Moderator
  • Thanks, Will Do
    Monday, July 6, 2009 10:02 PM