Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [A5] Keyboard state without a display

This thread is locked; no one can reply to it. rss feed Print
[A5] Keyboard state without a display
kenmasters1976
Member #8,794
July 2007

Is it possible to use al_get_keyboard_state() without a display or with no display focused?. I've tried and it seems it only works with a display and only if it is the currently active window.

Needless to say, keyboard events won't work without a display either.

Chris Katko
Member #1,881
January 2002
avatar

I've tried and it seems it only works with a display and only if it is the currently active window.

I think that's Allegro's choice of API (which is not atypical). You can only process EVENT's that are actually sent to your window because that's the normal "windows/GUI" expectation. There are global API's for keyboard events but it's not coming from DirectX, IIRC.

According to here, DirectX doesn't support global keys. But they could be wrong.

https://hydrogenaud.io/index.php/topic,63872.0.html

In my google searching "DirectX" and "global hotkeys" came up with almost no useful results.

[edit] If you have a few specific keys, there is RegisterHotKey. Otherwise, it looks like you hook into a low-level keyboard handler. Per this:

https://stackoverflow.com/questions/5259322/catching-global-hotkeys-windows

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Allegro's KB state comes from the window events. You need a window to get events. Hence, you need a window to get KB input.

Otherwise, you need to do what Katko suggested, and hook the low level keyboard process.

Just google code for a keylogger. ;) :P

kenmasters1976
Member #8,794
July 2007

Thanks for your replies. I wanted to use Allegro for a background process (no display) that would stop whenever a key is pressed. I guess it's not possible with Allegro alone, then, and I will have to look into those low-level routines. Thanks for the links.

Quote:

Just google code for a keylogger. ;) :P

Yeah, I guess that's kinda what I need, thanks.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

It looks like you might be able to capture key presses with WM_INPUT as well, even if the window is not in the foreground, as alluded to by :

Parameters

wParam

The input code. This parameter can be one of the following values.
Value Meaning

RIM_INPUT
0

Input occurred while the application was in the foreground. The application must call DefWindowProc so the system can perform cleanup.

RIM_INPUTSINK
1

Input occurred while the application was not in the foreground. The application must call DefWindowProc so the system can perform the cleanup.

lParam

A handle to the RAWINPUT structure that contains the raw input from the device.

So you might want to look into Raw Input :
https://docs.microsoft.com/en-us/windows/desktop/inputdev/raw-input

Low level hooks have some restrictions and caveats. IIRC, back when I wrote ManyMouse, I had to hook the low level mouse handler, but it had to be stored inside a dll for it to work properly, at least according to Microsoft.

kenmasters1976
Member #8,794
July 2007

Thanks for the link and sorry for the late reply.

I found this library which offers "A multi-platform C library to provide global keyboard and mouse hooks from userland". I'm currently trying it on Linux.

Doctor Cop
Member #16,833
April 2018
avatar

What are you trying to do?
???

Go to: