C# Consume Global Key Event
Asked Answered
G

2

7

I'm facing a tough one right now, at least it's tough for me!

I'm using this code to capture key press events and it works just fine even when the window is out of focus.

In addition though, I'd like to be able to consume the key event before it is used by any other application that does have the focus right now.

Search after search has brought no result for me, does anyone know how this might be accomplished?

Glider answered 24/7, 2018 at 12:4 Comment(19)
May I ask you why do you need this? (very_curious)Shoe
Haha I figured this would come up! We have keyboards for the MCDU of our Airbus Cockpit simulator, and those act like normal usb keyboards but I want to handle them separately.Glider
Username checks out o7Saltatory
Low-Level Keyboard Hook in C#Foxy
Would making it a service, help out? Trap all the events in there and hook the windows to always know which window has the foreground.Omniumgatherum
@RezaAghaei Does this actually include a consumer for the events? With my untrained C# eye I can not spot one.Glider
@Omniumgatherum Do you have an example for that? Unfortunately I am a Java programmer that just started with C# so all I really got from your comment was "Bananas!" :DGlider
@Glider Was gonna throw something together. But ... this article is awesome. c-sharpcorner.com/UploadFile/naresh.avari/…Omniumgatherum
@Glider You see _proc = HookCallback and SetHook(_proc). So HookCallback is the consumer.Foxy
@Glider Trap the messages and send them to any apps, you desire. Possibly, just broadcast a custom message, along with the MCDU events I'm looking to the community, for confirmation ... this is a good solution. But, sounds like it is.Omniumgatherum
@Glider Oh wait, does this software run on this MCDU or an actual Windows PC? I never made sure, you're running on Windows. Sorry. I'll delete my comments on the post, if so. That way, other people reading this post, don't get distracted.Omniumgatherum
@Omniumgatherum No, it runs on an actual Windows PC. The MCDU Keyboards are just normal USB Keyboards with a "special look". Current Status is: Capture a keypress, filter if it is from one of the MCDU Keyboards, if so, forward it via UDP to where the MCDU logic is. And then (or even before, i am not sure), because I can't consume it, the keypress event is also forwarded to whatever app has focus at the moment.Glider
@RezaAghaei I think we have a different understanding of "consume". I want to say that the event is not passed on to any other app, as I already have a hook and all that set up to capture the events. But then the keypress needs to disappear so that no other application is aware of it.Glider
@Glider Do you need a normal keyboard, in this app? If not, turn off all keyboard events, in your app(s). Capture your MCDU messages with the background app or service and broadcast them to your app(s). Your app just needs to process the received messages accordingly. See - Defining a WM_APP message, registering it, and broadcasting that messageOmniumgatherum
@Omniumgatherum unfortunately I do not write the other apps. Not all of them anyways, so I have no control over their behaviour when it comes to keyboard inputs. Unless there is a way to block a keyboard to send its commands to an app via windows, I am not aware how i could make them ignore the events. So I need to capture and consume them before they get them.Glider
@Glider I don't think, you can just block them easily. You can stop keyboard acceptance inside your app, with the KeyEventArgs.Handled = True, as mentioned below. I was hinting, at doing that and using a service or background app, to filter the MCDU events and rebroadcast them using that Custom WM_APP message. I mean, if you can pro grammatically enable and disable the device driver for the MCDU, without reboot ... or is it possible to enable & disable a USB port? Then, it'd only be usable when your app is running.Omniumgatherum
@Glider Enabling and Disabling a USB port : social.msdn.microsoft.com/Forums/windows/en-US/… - Question is .. Do you have to reboot? Don't think so. But, the MCDU would become available to other apps, besides yours. If you said, when my app is being used, nothing else will be getting used. Then, this would be a "good" solution. Just have to make sure, to shut it down, when not in use.Omniumgatherum
Take a look at SignalR, it sounds like a good use case. I found a project in GitHub that sounds very similar to what you are looking to accomplish github.com/michaelwda/OutsideTheBoxHubbard
I think you can Block Input from reaching to other applications after you're done with input data.Duodenitis
H
0

Im sure there is a simpliest way to to this (the code you use is old), but I've found this post. (Look at the code at the end)

Anyway, you can always use the 'KeyEventArgs.Handled = true;' property in the 'KeyDown' and 'KeyUp' event of your focused control to limite the key's propagation.

Hearst answered 29/7, 2018 at 23:49 Comment(0)
S
0

Here is the source code of a program that captures every key on windows, records it, and then passes it on to the next program.

You could take that code as a starting point and conditionally execute the call to CallNextHookEx to consume the keys.

Stickinthemud answered 1/8, 2018 at 4:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.