Windows Forms: simulate holding key down on keyboard - c#

During my search, I've seen many different versions of this question, yet somehow none of the solutions provided solved my problem.
It's really quite simple, I just want to simulate holding down a key on the keyboard through code. I want to try and make a character in a game walk forward constantly, so I just need to make a program that simulates holding down the 'W' key. I've seen a lot of people were using Windows Forms for this, I don't know if it actually is the right application but if it works I'm happy.
Just very quickly sending the key doesn't work, so simply calling SendKeys.Send('W') every 30ms does not make my character move in-game. So, what can I do to simulate holding down a key on the keyboard?

Here you go, a .Net library can simulate key press
https://inputsimulator.codeplex.com/
Edit
To get the key works, the external program must in active window, and your program need to run in background.
Windows Forms provides the SendKeys method which can simulate text
entry, but not actual key strokes. Windows Input Simulator can be used
in WPF, Windows Forms and Console Applications to synthesize or
simulate any Keyboard input including Control, Alt, Shift, Tab, Enter,
Space, Backspace, the Windows Key, Caps Lock, Num Lock, Scroll Lock,
Volume Up/Down and Mute, Web, Mail, Search, Favorites, Function Keys,
Back and Forward navigation keys, Programmable keys and any other key
defined in the Virtual Key table.
It provides a simple API to simulate
text entry, key down, key up, key press and complex modified key
strokes and chords.

Related

Certain keys can't be sent using keybd_event?

So I'm building an app that will eventually become a sort of docked On Screen Keyboard containing user-specified keys (part of my car-puter project).
So far so good, I can P/invoke keybd_event just fine for most keys, including Windows, print screen, etc.
The problem: certain keys such as VK_LAUNCH_MEDIA_SELECT and VK_LAUNCH_APP1 aren't working at all.
I'm using the same call as the other keys, so why is Windows being daft with just these "special" keys?
Further clarification: I'm not trying to capture keypresses, I'm sending them.
NativeMethods.keybd_event(this.Settings.vKey, 0, 0 | NativeMethods.KEYEVENTF_EXTENDEDKEY, (IntPtr)0);
NativeMethods.keybd_event(this.Settings.vKey, 0, NativeMethods.KEYEVENTF_KEYUP | NativeMethods.KEYEVENTF_EXTENDEDKEY, (IntPtr)0);
Where vKey is a char of a virtual keycode, such as VK_LAUNCH_APP1 (0xB6). I'm not sure if EXTENDEDKEY is really necessary since I don't think those specific keys are considered "extended" (i.e. Right-alt, Right-shift, etc)
VK_LAUNCH_APP1 (on Windows 10) will start File Explorer, no "Vendor provided program" needs to be present (other than Windows). Similarly, VK_LAUNCH_MEDIA_SELECT (0xB5) opens Windows Media Player (or whatever media player you have set in the Settings control panel).
You can certainly send these keys, it just isn't very likely that it does anything because nobody is listening for them. Look at your keyboard first, you'd need a dedicated button whose caption reads something like "Launch". Pretty uncommon, if available at all then it would at least have a "Play" and "Stop" button. Usually only available on a laptop, set aside from the regular keyboard keys and located above the function keys. Or overlaid on the function keys, you'd have to press the Fn key first.
The only kind of machine I've seen that had these keys was a Sony VAIO laptop. If available then you also have to first run the vendor provided applet that configures the key, determines exactly which program runs when you press the key.
Another vendor provided program, activated at login time, then recognizes these keys and implements their action. This requires writing a Windows hook with SetWindowsHookEx() for WH_SHELL, paying attention to the HSHELL_APPCOMMAND notification. Not the kind of thing you can do in C#.
Use the following code:
keybd_event(VK_LAUNCH_MEDIA_SELECT, 0, 0, 0);
keybd_event(VK_LAUNCH_MEDIA_SELECT, 0, KEYEVENTF_KEYUP, 0);

SendKeys alternatives?

I created a keypad app that stays on top, but does not take the focus so that on a touch screen it will forward whatever keys you press to the active application via SendKeys.
It works perfectly with every application I have tried it with... except, of course, for the one I actually need it to work with which is a Point Of Sale application. The POS application lets the user type in item codes on the keyboard but it doesn't have a good keypad for touchscreens so that's why I'm trying to create an external one for it (since I don't have access to the POS application code).
It actually does work when you first try it, but then it's pretty sporadic. Using the keyboard directly always works, so I'm not sure why SendKeys only works sometimes with this application. I've tried implementing it several ways... sending the keys as they are pressed, sending them altogether when the user presses the enter button on the keypad, copying the keys to the clipboard and then using send keys to do a Ctl-V and then Enter.
What other options do I have to simulate a key press to another application? SendKeys doesn't seem to perfectly simulate key presses, so is there a lower level mechanism I can tap into?
I should mention that when it doesn't work, what happens is that I get a beep from the POS application as though I'd pressed an invalid key. So it's not that it isn't getting some kind of input but clearly it isn't getting the key I'm sending it the same way it would from an actual keyboard.
I found this Windows Input Simulator: https://inputsimulator.codeplex.com/
Super easy to use and way, way, way better than SendKeys. And as a bonus, in addition to letting you simulate input, it also lets you set low level keyboard/mouse hooks.

C#/Monogame: shift keys blocked by arrow keys?

I often found useful informations here, now it was time to create an account on my own and say thanks to this community for it helped me so often!
I am currently coding a game in Monogame 3.2 in Mac OSX 10.9.5 using Xamarin 5.5.4 and encountering a strange problem I've never seen before: when I press one of the arrow keys it seems as if the shift keys, both left and right, are always considered to be NOT PRESSED, even if I press them.
I checked this with these lines of code inside my game loop:
foreach (Keys key in Keyboard.GetState().GetPressedKeys()) {
Console.WriteLine (key.ToString ());
}
The output is always the same. When I press one of the shift keys only or first (and hold), I get
LeftShift
RightShift
Plus eventually the arrow keys (if shift was hold first). But when I press one of the arrow keys first and then press the shift key while one of the arrow keys is down, those two do not appear.
That's obviously a problem because my game character now cannot run when shift is pressed (at least when the arrow keys were pressed first and hold). I double checked if other keys like Z are also blocked, but it seems as if the arrow keys only block shift (but I haven't checked every key so far).
Does anyone know why this happens? I checked whether or not sticky keys is activated in Mac OSX, but it's not. Sadly, I couldn't find anything on the internet so far.
Any help is appreciated! Thanks a lot in advance!
I think MonoGame is doing a few special things to get certain keys to work on windows, might be some of these same fixes need to be done on other platforms.
Try locating the code in MonoGame that translates from OS key events to XNA key events. It is most likely where the problem is.
Also try running the most recent develop branch just to be sure it isn't a bug that was fixed in the mean time.

How can I simulate function key combinations using C#?

I was hoping someone could help me with simulating a function key combination in C#, in this case FN + F4.
Here's what I'm trying to do... I have an older touchscreen netbook that I recently installed Windows 8 on. It has a swivel screen so you can fold it down and use it like a tablet. Windows 8 runs surprisingly well on it. I had a few minor issues, most of which I was able to work out, but one remains.
When you first power up the netbook or wake it from sleep, the screen is at 30% brightness, and Windows thinks that this is the max setting, so increasing the screen brightness through the normal methods does not work. On my netbook, pressing FN + F4, restores the screen to full brightness. This is a minor annoyance though, because I have to swivel the screen back and forth to fix the brightness using the physical keyboard, when I'd like to just leave it in the tablet position and minimize wear and tear.
What I'd like to do is write a little application that runs on start up, and simulates the function key combination, so the brightness is restored automatically. Any suggestions would be greatly appreciated. Thanks.
The Fn key is not a key that is sent from the keyboard to the computer. It is actually used to change the meaning of keys on the keyboard (such that the keyboard sends different scancodes when the key is pressed with Fn held down).
You should be able to easily write a simple Windows Forms program that listens to key presses and shows you what has been received. Register for the KeyDown event. The KeyEventArgs your handler receives has various properties that you can inspect to see what is being received.
The FN key isn't a real keyboard key like the other modifiers (shift, alt, ctrl) nor is it a function key like the F1 - F12. The FN key is a hardware only key that simply tells the hardware to send a different keystroke while the FN key is being pressed.
So, you are going to have to adjust the brightness manually like "m-y" stated in the comments or through some other mechanism.
Reference Link.

Emulate held down key on keyboard?

I know SendKeys can emulate typing and send individual keystrokes but I'm looking for a way to emulate holding down a key. My goal is an app that acts as a joypad for a windowed game. Imagine holding a tablet PC and having arrow keys and basic buttons on either side of the screen and pressing them emulates keyboard presses which a game then receives. That's my goal.
When a finger presses down on a button, I want to translate that to pressing down on a keyboard key. Upon releasing that button, I want to release that key. I know this'll probably require some low level code and I'm ok with that.
NOTE: I do NOT want to emulate these events in my own app, but system wide. I'm writing this for an XNA game of mine and it's not listening for direct, focus key events, it's checking the state of the keyboard, (as I assume most games do) and responding to that. I want my app to trick my game into thinking a key is held down at the proper times.
If you want to emulate it system-wide, you can use SendInput from Core.dll. There are more detailed information on how to do that here.
However, it is very rare, that you will actually need that. Instead, you should try to manipulate with only your own app (it seems more like that is what you need in this case). You can simply just make variables for all the keys, you want to read, and then read those variables from both keyboard AND tablet.
EDIT
oh, this link will probably be better...

Categories