Reading characters in a background application - c#

Hi I'm trying to make a simple program that read keys from keyboard even if my application is running in the background.
Situation
I want make a timer to help me in a game. I already have the program with the timer, the problem is I can not start the timer without switching of the game window to my app window. So I configured the game keyboard to release the keys F11, F12. Now in game this keys do nothing.
Problem
I built a windows forms containing a listener for keydown event and a conditional for F11. But when I trigger another window (eg the game window) my application no longer hears the keyboard, cuz it's in the background.
Question
How can I build a app that hears the keyboard, even if it's not active window?

You need to install a global, low-level keyboard hook using the SetWindowHookEx API call. Using the WH_KEYBOARD_LL hook will set your application up to intercept keyboard events at all times, even when your application is not active.
This post on MSDN shows an example of how to achieve something close to what you want from C#.

Related

C# How to send a hotkey globally (needed for a background process, running in system tray)

I am writing a program to automate some work for me.
I have another program (OBS Studio) installed, which I use to capture video. OBS Studio is minimized to the system tray and listens to my configured hotkey CTRL + 1.
When I press CTRL + 1 anywhere, the software starts recording. It does not matter which application is in the foreground.
I am trying to do the same thing from my application, send a "global" hotkey. I have spend hours trying to achieve this, but without any result. SendKeys only works for the current open window (which does not exist), no results with PostMessage either and I tried the wrapper "InputSimulator"
So, to summarize:
Is it possible to send a hotkey/keystroke globally (for every application?)
If not, how would I send a hotkey to a program that is running in the background, without a window? I don't want to bring the app to the foreground.
Hopefully someone with a deeper understanding of these concepts can guide me...
I've found similar questions, but they remain unanswered:
Send global keystroke / fake a global hotkey from a Winforms application
Sources I studied (among others):
https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys(v=vs.110).aspx
https://dailyoverdose.wordpress.com/2009/10/28/postmessage-and-sendmessage/
https://ourcodeworld.com/articles/read/520/simulating-keypress-in-the-right-way-using-inputsimulator-with-csharp-in-winforms

Control taskbar blinking event of an other application

I want to build a c# desktop application which checks the taskbar blinking event of a specific program. When ever the programs taskbar blinking event occurs my application will play an alarm sound. Is it possible and where to start please ?
One way would be to use something like EasyHook and basically replace the processes call to FlashWindowEx function.
However because you don't directly control this application it is likely to be error prone.

How to simulate mouse and keyboard to operate an application from a WinForm program?

I am writing a WinForm program in C#, to move mouse cursor, click mouse, stroke keyboard, to operate another running GUI application in Windows.
Is this possible, how to do it? I mean the simulation of mouse and keyboard and operation of another GUI application.
Thanks a lot!
SendInput is your friend here. It doesn't have a direct C# wrapper, but it's easy to use with P/Invoke.
SendInput places input into the input queue, which then gets dispatched normally, so you will need to set focus to the target application first.

Starting application from background process

I'm working a small WPF based program for launching applications through system wide hotkeys implemented using hooking. I'm implementing it in C# and Visual Studio 2010.
When I detect the specific keypress I use Process.Start(...) to run the application. This works fine while Visual Studio is active, placing the new application in the foreground with input focus, as I would expect. If my launcher is in the background (behind another active program), it still detects the key and starts the application correctly, in front of everything else.
The problem is, that when I run the launcher without Visual Studio active, and my launcher application isn't front, neither will applications it starts. They appear in front of the launcher but behind the active application.
I can see that other software, like AutoHotkey, is able to do hotkey launching with this behavior, but I fail to see what I'm doing wrong.
Update: Just figured out a solution to this issue that works in my development environment. I first register a global hotkey through the Windows API RegisterHotKey using the launcher main window handle. The key could be any, but should be one that normally doesn't exist physically, F24 in my case. Then, whenever I detect a keypress through the hooks that should launch an app, I first call keybd_event to 'fake' a keypress for the hotkey.
For WPF use:
keybd_event((byte)KeyInterop.VirtualKeyFromKey(Key.F24),0,KEYEVENTF_KEYUP,0);
For WinForms use:
keybd_event((byte)Keys.F24,0,KEYEVENTF_KEYUP,0);
This will bring enough focus to the launcher, so that Process.Start(...) makes the executed program get in front. It does not bring the launcher window to front, nor does it make the launcher accept inputs.
If Activate() is called on the main window after the keybd_event(), this will bring the main window to front and allow for keyboard input, just as if the user had task switched.

Emulate Key Presses On an App that takes no Win Messages

I want to send an Application Key Presses, To Automate some stuff that has to be done repeatedly and So I don't always have to cramp my fingers.
In C#, it's nice to use SendKeys.Send(), but this won't work because the Application doesn't take Windows Messages. SendKeys.SendWait() does nothing at all.
How would I STILL Simulate the Keyboard events?
Come To Think of It, I was going to use some P/Invoke to simulate Mouse Events too, but If it takes no messages, How Can I get around that?
EDIT - I can use mouse and keyboard to interact with the program, I just cannot manipulate it with Windows Messages sent from my own Code.
Have you tried AutoIt?
Is it a console app? If so, maybe you should be SendKeys'ing to the command shell instance it is running in.

Categories

Resources