Detect/Block SendKeys.Send key messages - c#

I was wondering if there was a way to detect if key strokes are from the keyboard or a SendKeys.Send call.
I would like to block specific SendKeys.Send keys but if those keys are directly from the keyboard then let them go.

Create a console application and run the code located here: http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx
Create a second application that will fire SendKeys.Send when you click a button. You will notice that the first console application detects key strokes from the keyboard but not keys from SendKeys.Send
I'm sure you could you use the code from the article to detect if the keystroke came from the keyboard or SendKeys.Send

Related

How to use SendInput key_down and key_up events without blocking thread?

I'm sending keys to a game. It works well but that i use thread.sleep blocks thread so i couldn't send keys from actual keyboard spams along code runs belong:
Keyboard.SendKey(Keyboard.DirectXKeyStrokes.DIK_2,false,Keyboard.InputType.Keyboard);
Thread.Sleep(100);
Keyboard.SendKey(Keyboard.DirectXKeyStrokes.DIK_2, true, Keyboard.InputType.Keyboard);
Thread.Sleep(100);
...(repeat something like that 20-30x)
Keyboard class comes from this link (it uses SendInput key down and key up events.)
But if i run this, it blocks hardware keyboard inputs. How to run this and other key strokes async or parallel. If i don't use thread.sleep it doesn't work in the game so i should use it. But how can i achieve this without blocking sendinput thread?
EDIT-1: It's a WPF app. But it doesn't matter WPF or Windows Forms
app. Because i am trying to make a macro and it's been triggering by a
play/pause key. I also use a global keyboard hook (low level) so i catch
play/pause keys.
EDIT-2: If i don't use any delay between key down and up, the game
doesn't recognize key pressing. Also Key strokes has a sequance and
the sequance has different delays (key_2 100ms, key_3 500ms, key_a
200ms etc).
EDIT-3: Key strokes must be inputs instead messages. Because the game recognize > messages as a text and do not run skill functions. So i use SendInput instead
PostMessages.
Thanks in advice!

C# Recognize keys pressed when not in focus

I'm making a program that will shutdown a .exe app when F1 is pressed! I did it, but I'm trying to do it without having to focus on the form! How could I do that?
You can set up low level keyboard hook, it will notify your app with key press events. Check this article, it's almost 10 years old, but should give you a hint. Basically the key thing is to import native method SetWindowsHookEx and process key presses with your callback delegate. Last time I used it was on Windows 8.1, so I believe it's still actual for Windows 10.
For this case, you can use NotifyIcon component along with the form. Add a context menu with a shortcut key (F1 is not supported I think, you need modifiers) which then finds the PID and terminates that process.

TAPI, C#: Detecting key presses on a phones keypad

I'm attempting to set up a system where I phone a number, and then to confirm it isn't an answering machine I want the recipient to press a phone key to forward the call. The trouble is I'm struggling to find the event that gets raised for phone key presses.
I imagine it is a tone event but nothing seems to be raising.
I could also do with knowing how to detect what key is pressed as well.
Thanks,
Ryan
These are called DTMF tones, TAPI can detect and report them via the ITDigitDetectionEvent interface.
You need to setup the appropriate event filter and tell ITLegacyCallMediaControl::DetectDigits that you are looking for DTMF.

How to simulate Keystrokes in C#?

Google gives this but it seems to only work for a WindowsForms class. I'm completely new to C#. What I'm trying to make is a program that monitors for an event, and then (for example), acts as if the 'H' key has been pressed. I don't really want to worry about what the active window is or anything, or sending a keystroke to an application, I just want the program to act as if I have physically pressed the 'H' button on my keyboard. The SendKeys class doesn't seem to work in a general class. Am I going about this completely the wrong way?
From How to: Simulate Mouse and Keyboard Events in Code:
Windows Forms provides several options for programmatically simulating mouse and keyboard input.
Simulating Mouse Input
The best way to simulate mouse events is to call the OnEventName method that raises the mouse event you want to simulate.
Simulating Keyboard Input
Although you can simulate keyboard input by using the strategies discussed above for mouse input, Windows Forms also provides the SendKeys class for sending keystrokes to the active application.

Problem with sendkeys in .NET

I have a windows application, where I am using send keys to navigate from one window to another. My send key sequence is like activating another window with in my app, and sending key strokes to that window. But the key strokes I am sending is getting updated in the same window where I am activating the other window.
But after few key strokes it is going to the other window. Seems like synchonization issue with send keys. Is there a way to specify the operation of the current send key is finished, before sending the next sendkey.
I have found sendkeys to be nothing but unreliable. Try the input simulator instead.
Have you tried using SendKeys.SendWait. This will wait until the key was sent and the action processed before carying on.
Since the windows are in your own application, why not provide set of functions to carry out the actions rather than using SendKeys. Functions can create the window once the windows in created and active which you can detect by handling the Activated event, you can then use SendKeys to send key strokes to the new window, if you really need to, otherwise again have functions on the window class that you can call to carry out the operations that the key strokes would induce. You could probably define a common interface that your windows implement to facilitate this interaction.

Categories

Resources