0

I am working on some code in a DLL for a project. I use this in Java but the question is about C or C++ only.

When using java I used Robot to do keypresses but I quickly encountered the limitations about this. So I now use JNI to do keypresses via a C library.

In the C library I tried the keybd_event which I upgraded to SendInput and they work in almost every situation besides one. And let that one be the reason why I needed the C library in the first place.

In a program X you can set up key combinations like Ctrl+Shift+A. If you use my code when X is in the foreground it works perfect. But when you have a different application in the foreground it does not. The problem however is that when I do this not with my DLL code but just with the keyboard it works in both situations, leaving me with the idea that there may be better methods to call keypresses in windows.

What functions exactly mimics the keyboard in windows?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Ghotirey
  • 103
  • 2
  • 9
  • 1
    Foreground application is defined exactly as the one that receives such events. What do you want to do exactly? – Jean-Baptiste Yunès Feb 01 '18 at 10:52
  • Well Application X is not in the foreground but still reacts on my keyboard key combinations. So the keys are somehow also interpreted outside the forground application in windows and are passed to the not in foreground application, the result is that the key combination is triggered in an app which is not in the foreground. SendKeys/SendInput does not show this behaviour. – Ghotirey Feb 01 '18 at 10:55
  • I don't think the problem is with your sending events; but with your listening to them (in that your application needs to be listening to keyboard presses sent to all apps rather than just itself) – UKMonkey Feb 01 '18 at 10:58
  • Something like `EventManager.RegisterClassHandler(typeof(System.Windows.Controls.Control), System.Windows.Controls.Control.KeyDownEvent, a delegate function);` ? – Jean-Baptiste Yunès Feb 01 '18 at 11:06
  • @UKMonkey That should not be the case as Application X is not developed by me. And it works with the keyboard so there is a difference between keyboard and sendkeys. – Ghotirey Feb 01 '18 at 13:13
  • @Jean-BaptisteYunès I believe that is a dot net function? Since everything is else is Java and only the DLL is in C (or C++) I would really want to know how this function differs from the ones I tried, I did not know it was possible to use .net in a java environment. Will look into this – Ghotirey Feb 01 '18 at 13:25
  • Basically: send raw keyboard scan input. If you send already interpreted keyboard input, the hot key translation is skipped too. – MSalters Feb 01 '18 at 14:16
  • @MSalters, thank you very much, I searched the internet for scan codes and it seems, although not documented very well, that you can use SendInput to bypass the virtual keys and use scan code instead. Thanks for the info! – Ghotirey Feb 01 '18 at 16:28
  • So I have now tried it but it seems that the raw input still does not do what I need. The raw input allows me to use the keys, not the virtual, but the actual, but still the key combi is not triggered in the external program that is not in the foreground. If I use the keyboard it still works, also if i bring the program to the foreground, the raw works. – Ghotirey Feb 10 '18 at 16:15

0 Answers0