A general solution to injecting keys to different applications on windows? - windows

After receiving some feedback on this question: How to create lParam for WM_CHAR or WM_KEYUP/WM_KEYDOWN?, I`ve started looking for a broader answer to a general solution. One thing I realized that using windows API's might not work for every app and in every case.
My first step in the follow up research was to make an Arduino powered servo to press the keys (yeah the concept is horrible ik).
But that prompted yet another idea, a hardware augmented small numpad keyboard which is also operated by Arduino which was controlled via another usb. This was at least somewhat usable - but still not very.
Then I tried to use Digispark Atiny85 microcontroller which in turn used Digikeyboard library. This solution was much better - but then necessity to have a digispark stuck in your usb port was a bit frustrating.
This made me curious if there are ways to emulate keyboard or any other HID devices using software only? Some brief googling pointed me to Kernel drivers and virtual COM ports, but that seem to be a bit over the top for me to process.
So can that task indeed be achieved by writing a kernel driver? Can it be done in any other manner? In either case are there any pointers which you can give me on the topic?

The SendInput function can be used to generate keyboard and mouse input. This input goes to the foreground window as if generated by real hardware (but lowlevel hooks can tell that it was software generated). It might not let you generate Ctrl+Alt+Delete nor control a UAC prompt but other than that it should be good enough in most cases. Writing a driver to overcome these limitations is normally not worth it.
There is no general way to generate input to a specific application/window if it is not the foreground window.
If you want to control a specific application you should use UI Automation.
Faking key up/down/char messages with PostMessage is not uncommon but it does not always work (the application might be using RAW input, input is not synchronized with real hardware etc.). If you are determined to use this method anyway, make sure you send it to the correct window (the HWND with the keyboard focus, not just the top-level window). Use the Spy++ tool to view the messages to make sure they are going to the correct window.

Related

Detect disconnect/loss of hardware components (Windows 7)

Scenario: A critical computer system is operator-controlled via standard USB keyboard and mouse. Also, there is a DVI-monitor connected to view the operator-targeted GUI. The computer system runs a soft-PLC system based on Windows 7 Professional or, alternatively, Windows Embedded Standard 7 (the "system software").
Question: Is there a software solution, to detect the loss (disconnect/failure) of USB HID-devices such as the keyboard or mouse, and the single DVI-display? This is important, since the critical system can no longer be expected to function properly, without the operator able to manipulate it or see displayed content.
Own considerations: This likely requires low-level WINAPI calls, which is fine. I am thinking that a windows service might be constantly seeking to enumerate the number of keyboards and displays - perhaps even identify them via model or serial number. If this enumeration and/or identification reaches zero or fails entirely, the system-software must of course react fast and appropriately (i.e. go to fail-mode or similar).
As far as I see it, this is general issue with all critical operator-controlled systems. Question is then: Is there already software or hardware for this in existence perhaps?
Note: Operator is always human.
Alas, as for an answer this isn’t going to be much more than a “read the docs” plus some links... Sorry.
First, MSDN documentation.
RegisterDeviceNotification
Detecting Media Insertion or Removal
Talking to USB devices, start to finish (Windows Store app)
I found a C# class on CodeProject.com that does this; the accompanying article is pretty good.
Detecting USB Drive Removal in a C# Program.
I admit that the last time I did anything like this was some years ago, and only for CD notifications. I’ve since lost the code (both my primary and backup hard drives failed within days of each other, LOL).

Typing simulator — sending keystrokes to other application

i've been challenged to write an app simulating keystrokes. After pressing shortcut app should be able to send predefined key combination to currently active app. It is functionality provided by many existing applications, but i want to write it on my own. App should be usable on windows.
Could you provide me with suggestions about:
which programming language should I choose?
Is there any libraries providing such functionality?
EDIT: To be precise: both applications are standalone, windows app.
The native winapi for doing this is SendInput.
To answer your questions:
Which programming language? This is up to you. Because this is just a simple native API call, use any language that allows you to call native API.
Don't bother with libraries if this is all you need, because it's very simple.
Now, to go further, I know you didn't ask this but many many people continue by asking how to send keystrokes to windows that don't have keyboard focus - like sending keystrokes to a specific app. This is much more difficult and error-prone. And since it goes outside the behavior of actual real keystrokes, it can behave unpredictably. Here is one such question.
You can easily do this on C#, using
SendKeys.Send("key here");
or
SendKeys.SendWait("key here");
Some keys use other keycodes, you can see them here on MSDN.

Intercepting mouse events in windows

Well the title seems pretty clear about what I want to do.
More precisely: I want to create a program (c++ or java is preferred) that manipulates the mouse in two ways, like: changing its position and doing clicks.
I was thinking about using allegro (it has mouse routines to manipulate the things cited above) or sdl(which I don't know if has that kind of routine). I tried with allegro nsuccessfully. My problem here was that I couldn`t virtually "do" clicks. I also couldn't redirect the stuff changed by my program to some other window.
Any tips?
There are a couple of ways to try automating other applications on Windows...
At the simplest level, one can use PostMessage to post keyboard and mouse messages to another application's windows. This has the advantage that it could work even if the other application is minimized. Unfortunately, this approach skips the majority of the input processing logic so applications that directly access key state using GetAsyncKeyState will not see (for example) the control key being 'down' no matter how many WM_KEYDOWN, vk=VK_CONTROL messages you send.
As Hans Passant commented, SendImput places input events in a lower level input event queue, and so can fully simulate modifier keys. These input events are not posted to windows however, so to get the input events delivered successfully the normal windows rules of activation and focus need to be followed. That said, this is the approach used by most testing-automation software (and is why most testing automation software requires the application being tested be the active application).
The last of the automation methods Ill mention - and sadly the least likley to work - is the Microsoft UI Automation framework. This framework is intended to allow applications to be used by disabled and/or special needs users. Sadly - very few software providers bother to implement this API in their products.

keyboard hook in windows C++ or what?

I wish to build my own application which can send keyboard commands(messages) to the Windows OS.
For example when I press the combination ctrl+shift+n, I wish to launch the notepad.exe . How can I do that? Do you have some advice for me about the concept used.
I've read that is possible when are used keyboard hooks? That's the only way? Do you know a free and open-source application which does this as simple is possible?
Your particular example can be done without any programming at all, by right clicking on Notepad, selecting Properties, and setting the "hot key" (various Windows versions might call it by a different name) to Ctrl+Shift+N.
If you still would like to write a program to do this, have a look at the RegisterHotKey Win32 API function.
AutoHotkey is a free, open-source utility for Windows.
You can automate many tasks with the above utility, check it out.
Things to bear in mind:
A system-wide keyboard hook requires the writing of a DLL. There's example keyboard hook code on my website here.
Hooks cannot be installed from a low to a high integrity level application in Vista and Windows 7/8/10. So there's no guarantee your hook will work, depending upon what the foreground application is when the key gets hit.
As Greg pointed out, a lot of the time, RegisterHotKey is a much simpler solution for this problem.

Taking use of laptop custom buttons

I have a Lenovo Y550 laptop which has a nice looking touch sensitive strip with led lights on top of keyboard. The usage for this is however quite useless (it can be used to start 4 different Lenovo programs) so I started to think if I could program something of my own for it.
However I don't have any experience with this kind of thing.
First of all I'd like to know if it's even possible to use it on my own program in any way (capture touches or even control the lights).
Second, where should I start researching about this? I checked Windows Device Manager to see if I could spot anything helpful there, but no success. I can only see many kind of HID devices there.
One thing that is on my mind is to use some kind of hooks to take usage of this. Could that work? I don't really mind what language I'll have to use, learning new ones is a useful anyway.
If it is possible to totally control the touch sensitive strip it would be nice to light up the led lights as I will on it (now led light lights up where my finger is).
I did something similar for my Logitech keyboard. I had to hook the low level keyboard hook, and I modified the Logitech specific key codes into a Windows generic one. All in all a very simple, short and limited tool, without any configuration, but it did its work reliably and only used a few kb or memory.
Get a low level keyboard hook working, set a breakpoint and see what key codes you get. Don't know about the LEDs though.
Edit: Found it. It's been quite a while since I wrote this.

Resources