Background Key Press Listener

Hi again,

I just wrote this, and it dissapeared, so I'll write it again and hoepfully it won't turn into a double thread.

I basically want to my program sitting in the background doing it's own thing, or just idling about, and be able to press a key and have this key jump to a function (or perform some action) in my background program.

I've looked at RegisterHotKey, but from what I understand, this just puts a message into the message stack. I would rather jump to a function without having to contanstly check the stack. Even something that triggers when the message stack is added to is fine. I basically want a Java type ActionListener.

Anyone have any ideas or direction It'd be VERY appreciated... I'm ripping my hair out over this whole thing.

Dan.

P.S. Sorry about all the "I want" in my post... makes it sound so forceful :(



Answer this question

Background Key Press Listener

  • hey_ian

    From what I have gathered, you can only use RegisterHotKey with a windowed program. So for my DLL and console based programs, I can never use HotKey's. Without these, and no event based or callback functionality for key presses, I can't see any other way to respond to keys being pressed without a program window other than constantly looping and checking yourself.

    Although it is CPU intensive, I've resigned to the fact that this is the only way.


  • december_i

    I ended up having to just do this in a windowed app, and going ShowWindow(hWnd,SW_HIDE); to keep the window hidden.


  • mendi

    I can't see anywhere how to do it better. Any suggestions are very welcome.

    Also, is it the Sleep that eats battery If I put it into Sleep for an hour, would that chew through battery


  • Jianxing

    No takers on this one I guess I'll keep looking myself.
  • msaradhi

    I've still found no real solution for this. Is there some way to set a user notification to act on it, or anything like that
  • robinjam

    The problem with this is that it'll eat your battery like crazy

  • Gengis

    Even though is extremly poor coding, I've implemented my own key press detector as follows. It just makes sure the key was held down for roughly 4 seconds:

    void keyPress()
    {
    if (GetAsyncKeyState(32) != 0)
    {
    keyTimer++;
    }
    else
    {
    keyTimer = 0;
    }
    if (keyTimer >= 8)
    {
    // In Duress
    MessageBox(NULL,L"You have pressed space.",L"keyPress",MB_OK);
    duress();
    }
    else
    {
    // Sleep for half a second, and check again
    Sleep(500);
    keyPress();
    }
    }

    It's not very good, but I guess it'll work without too many dramas. I think.


  • Background Key Press Listener