Send keypressed to an app - c#

I'm trying to send virtual button presses to an app. I tried using SendKeys.Send("{6}");, but the app receives it like a string or something similar because it doesn't act. The app I want to send the key to is VisualBoy Advance. My code simply allows me to play remotely using a GUI, so when I press left on the GUI the VBA should receive the keystroke.
A part of the code:
case "derecha":
SendKeys.SendWait("{4}");
break;

The { and } characters are special escape characters. I don't think 6 is an escape value. Have a look at http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=vs.110).aspx for the known escapes. For example, SendKeys.SendWait("{Right}"); would send a right arrow key.
However,
Many games and emulators will be using low level keyboard hooks to read the keyboard state. It's quite rare to find games that use the Windows events model for keyboard reading. SendKeys only sends to the Windows messaging system. You may need a more low-level way of sending events.
If the target app is using DirectInput, you might be able to use the SendInput function from user32.dll:
Docs - http://msdn.microsoft.com/en-gb/library/windows/desktop/ms646310%28v=vs.85%29.aspx
Example - http://www.ownedcore.com/forums/mmo/warhammer-online/186390-sendinput-example-c.html
There is also the Interception library, but I've never used it myself:
http://oblita.com/interception.html

Related

C# Hooking specific Keyboard keystroke and stop system propagation

I have an application where I have multiple keyboards connected through USB.
I need to hook a specific keyboard to get the keypress directly into the software, even if this one is not on the foreground. This so far works based on this project. The other keyboards shall work as normal.
Although it seems by using Direct Input, it is not possible to stop propagation (we don't want other apps to get that particular keyboard input).
Concerning Global Hook, it can block the keypress system-wise, but it is impossible, as far as I know, to identify the source of the keystroke (which keyboard it is from) and thus to selectively block them.
There is another project, here, that combines the two, but it is quite messy and heavy.
Is there a better way to achieve this? I am surprised that simple task is so complex.
If you are going to make the app windows only, you should look into Windows raw input api
It isn't that complicated.

How to retrieve a string in a C# Key(*)Event that was inserted by scan? (Windows CE 5)

I am working on a Win CE 5 application that captures data scanned via barcode scanner. The application should support some "system-barcodes", predefinded barcodes that trigger functions of the application.
Those barcodes have to work in the whole main-frame. So I set the property "KeyPreview" of the main-frame to true and registered an KeyEventHandler on the KeyDown event of the main-frame.
My problem is, I couldn't figure out how to get the full scanned string. The string is 12 characters long and ends with a newline.
Is there a possibility to get the whole string in the EventHandler?
Thank you in advance for any help.
Most, if not all, barcode scanners based on Windows CE inject the scan data as keyboard data to the device driver. The easiest way to intercept that data in your app, where you don't have to hook up handlers to every form and worry about controls getting the keys before your processing logic is to use a keyboard hook. It works just like on the desktop, so any code you find that applies to keyboard hooks on the desktop would be valid (with the exception that the DLL containing the APIs in WinCE is coredll.dll).
There is an example of keyboard hooking for Windows Mobile on CodeProject which would probably give you the bases for everything you need. From there, it's simply string parsing in the hook handler.
Using any of the Key events by setting the Form.KeyPreview property should allow you to see the data from the scanner. But you will only see it one "key" or character at a time. Those events only handle a single character or key on each call. As far as you app could tell, it looks no different than a user smashing keys on the keyboard.
If you have the option, and can put your scanner into a non-keyboard emulation mode and hook into scan events directly, then you would probably be much more satisfied with the results in terms of how it can work independently of the GUI when used that way. Typically with devices that can do that, you will receive the entire scan as a single event.
I'd suggest including the details about the device(s) you are using so someone might be able to give you more specific advice that might be relevant for the hardware in question.

Is it possible to block keyboard input selectively in Windows? How?

My app (in C#) need to interface with a USB bar-code scanner, which is basically working like a keyboard. It inputs the bar-code with an enter key at the end.
The app need to be work even when it's at background, so I am using low level keyboard hook to get and filter the bar-code out in the global key events. This part is already working.
Here is my problem: I don't want other apps to get the keyboard(scanner) inputs if it is a bar-code. And the normal key events should not be interfered. In one word, block the key events selectively. Is this possible?
My app is in C#, but I have no problem with C++ or more native solutions as long as it's easy to integrate in C#.
Thanks.
Additional Information:
The whole idea is working at background, even when it's not active. It watches the global key events stream and spot the bar-code sequence (already implemented with Hook). And most importantly, it do NOT interfere with normal keyboard events nor other applications' operation. That's why I cannot block all the key events or make it top-most.
I already can get the bar-code. I need to prevent other applications from getting the bar-code.
At the end of your keyboard hook you would call CallNextHookEx to execute next hook in the chain.
I would suggest that put some unique signature as a preamble for your barcode so that your keyboard hook procedure can detect it as a valid barcode input from your scanner. Now, when you get this data, just skip the call to 'CallNextHookEx' so that the chain will be discontinued and other programs won't get your barcode. Otherwise - call 'CallNextHookEx' so the chain can continue.
Note: This is my theory, I have never tried the exact same thing myself. I have however, written hooks in C++ and C#.
Check this project out
http://globalmousekeyhook.codeplex.com/
It is in C# as well so will make your coding easier. Sounds like all you need is to hook up the global key press event and suppress it by setting the Handled value or something similar.

Application Switching with a Global Hot Key on Windows Mobile 6.1/6.5

I'm working on writing an application that straddles the line between C# and C/++ on Windows Mobile 6.1/6.5. We currently have a kiosk application running on our devices, and would like to add the ability to switch back and forth to a second kiosk application.
Our goal is to establish a global hot key that switches process windows (similar to the way that alt+tab works) whenever it is pressed. We already have both applications and I've written some code that switches the processes, but am having a rough time getting the global hot key portion of the project working.
From all of the reading that I've done, my understanding is that the best way to monitor global key presses is to link into the system message pump with the SetWindowsHookEx function in coredll.dll. Unfortunately, I've also read that this function isn't technically supported on the platform.
I also found some tutorials that suggested using a message map with the ON_WM_KEYUP/ON_WM_KEYDOWN macros in the MFC framework, but couldn't find any documentation specific to Windows Mobile. When I tried to use the documentation here, my device kept crashing.
Is there an accepted best practice for setting some kind of global key hook on the platform? If not, is there something that's at least technically supported?
Thanks in advance.
ReplyQuote
Why not use a RegisterHotKey call and use that to swap applications? IIRC the hardware buttons typically map to key codes starting at 0xC1 (193).
We actually ended up polling the GetAsyncKeyState function in coredll.dll on a separate thread. The thread monitors a specific key, and throws an event whenever it is pressed.
Because the event is executed on the key polling thread, you have to be sure to use a delegate to invoke its handler on the GUI thread when the event is thrown.
I would go for a keyboard hook, but only if RegisterHotKey didn't work for your particular scenario.
From all of the reading that I've done, my understanding is that the best way to monitor global key presses is to link into the system message pump with the SetWindowsHookEx function in coredll.dll. Unfortunately, I've also read that this function isn't technically supported on the platform.
Not technically supported, is correct in theory, but I've not seen a WM 6.5.* device that hasn't supported it in reality. Keyboard hooking is such an important feature of vertical market custom rugged WM device apps that it I think it just cannot be removed, for backwards compatibilty.
The enterprise side of the WM space is too important.

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