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?

Kit
  • 20,354
  • 4
  • 60
  • 103
Maverick283
  • 1,284
  • 3
  • 16
  • 33
  • 2
    May I ask you why do you need this? (very_curious) – Renatas M. Jul 24 '18 at 12:12
  • 1
    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. – Maverick283 Jul 24 '18 at 12:21
  • 2
    Username checks out o7 – nilsK Jul 24 '18 at 12:54
  • 1
    [Low-Level Keyboard Hook in C#](https://blogs.msdn.microsoft.com/toub/2006/05/03/low-level-keyboard-hook-in-c/) – Reza Aghaei Jul 27 '18 at 02:21
  • 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. – tobeypeters Jul 28 '18 at 03:05
  • @RezaAghaei Does this actually include a consumer for the events? With my untrained C# eye I can not spot one. – Maverick283 Jul 28 '18 at 15:28
  • @tobeypeters 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!" :D – Maverick283 Jul 28 '18 at 15:29
  • @Maverick283 Was gonna throw something together. But ... this article is awesome. https://www.c-sharpcorner.com/UploadFile/naresh.avari/develop-and-install-a-windows-service-in-C-Sharp/ – tobeypeters Jul 28 '18 at 15:46
  • 1
    @Maverick283 You see `_proc = HookCallback` and `SetHook(_proc)`. So `HookCallback` is the consumer. – Reza Aghaei Jul 28 '18 at 15:56
  • @Maverick283 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. – tobeypeters Jul 28 '18 at 16:01
  • @Maverick283 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. – tobeypeters Jul 28 '18 at 16:09
  • @tobeypeters 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. – Maverick283 Jul 28 '18 at 16:43
  • @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. – Maverick283 Jul 28 '18 at 16:46
  • @Maverick283 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 message – tobeypeters Jul 28 '18 at 19:15
  • @tobeypeters 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. – Maverick283 Jul 30 '18 at 12:00
  • @Maverick283 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. – tobeypeters Jul 30 '18 at 18:09
  • @Maverick283 Enabling and Disabling a USB port : https://social.msdn.microsoft.com/Forums/windows/en-US/6bc7ceca-4591-42ba-a483-b313d3226ba0/how-to-disable-usb-port-in-c?forum=winforms - 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. – tobeypeters Jul 30 '18 at 18:18
  • 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 https://github.com/michaelwda/OutsideTheBox – Charles Jul 31 '18 at 19:48
  • I think you can [Block Input](https://learn.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-blockinput) from reaching to other applications after you're done with input data. – Avestura Jul 31 '18 at 22:25

2 Answers2

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.

Guibi
  • 115
  • 1
  • 7
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.

cat_in_hat
  • 635
  • 9
  • 15