how can I press "Enter" on c# and WPF without pressing enter on hardware keyboard ?
Ah, so you're saying SendKeys is unavailable?
Check these threads, on the MSDN forums: http://social.expression.microsoft.com/Forums/en-US/wpf/thread/915cb53a-704b-4047-8fd0-e7c5a8feae5e/
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/6557c1eb-6eb5-4f4a-84d6-4ae4fb5b9dab/
Sounds like you can trap KeyDown events, but that's not exactly what you want...
Oh, wait! Check this SO thread: How can I programmatically generate keypress events in C#?
Beyond that, How and why do you need to send "Enter" without a keyboard press? There may be a different solution lurking there. :)
Assuming that you're working with WindowsForms... by using SendKeys.Send("{ENTER}");
See this documentation here as well: http://msdn.microsoft.com/de-de/library/system.windows.forms.sendkeys.send.aspx
Related
I'm trying to make a game in WPF, C# where it would be crucial to have quick movements with the keyboard. Does anyone know how to get rid of the said phenomena?
What you are likely seeing in terms of a 'delay' is the keyboard repeat delay, which is controlled by the operation system (see Control Panel -> Keyboard).
You will probably have better luck working with the KeyDown and KeyUp attached events. https://msdn.microsoft.com/en-us/library/system.windows.input.keyboard.keydown(v=vs.110).aspx
By handing these events, you can handle repeating the functionality as frequently as you want until the KeyUp event is received.
I am currently making a web browser in MonoDevelop using C#, but I am having problems implementing the Keypress event.
I already know the code for how to make the web browser navigate, but the problem is that the keypress event for the enter key won't fire in the textbox.
I have tried other alphabetic keys (like Gdk.Key.a), and they work properly, but the enter key does not work.
I have also tried add [GLib.ConnectBefore] attribute before the keypress event, but it still doesn't make a difference.
Can someone please give me the whole code, if you don't mind? Because there are three different types of Enter keys in the Gdk.Key function, and I don't know which one to use.
I am using MonoDevelop 2.6
Thanks for your help
I have figured out how to solve this problem.
I was using the wrong event.
You do NOT use the OnKeyPress or OnKeyRelease events to get the signal of the Enter key. You would need to use the Control.Activated event.
The Activated event fires only when the return (enter) key is pressed.
I hope it's the same for every one!
Thanks for trying to help!
Is it possible to programmatically create input events in c#.
I want to be able to simulate any input, such as pressing a button on the keyboard or moving the mouse pointer from my application. The events generated by the application need to behave (preferably) in exactly the same way that an actual key press for instance does (post an event to the system event queue that is given for applications to process).
I seem to be unable to find any documentation about doing this from c# code after googling for an hour.
As always, any answers will be greatly appreciated.
You can try SendKeys for keyboard input. For more control, and mouse events too, you should P/invoke SendInput().
Be warned that this is difficult to get right and you may find alternative approaches more amenable.
I use PostMessage to simulate keystrokes in a program that is in the background. It work fine except for characters that need shift on the physical keyboard. How do I simulate shift? "
The code I use is roughly:
VK vk = VkKeyScanEx (c, GetKeyboardLayout (0));
AttachThreadInput (_attachedThredId, _attachedProcessId, true);
PostMessage (_window, WM_KEYDOWN, vk.key, 0x1);
PostMessage (_window, WM_KEYUP, vk.key, 0xC0010001);
AttachThreadInput (_attachedThredId, _attachedProcessId, false);
How should I handle Extended part of VK?
Edit
I'm trying to create an on-screen keyboard. Each button on the on-screen keyboard simulates a sequence of keystrokes. The receiver is an old program that performs different tasks depending on the keyboard sequence that is performed.
Keyboard sequences is as follows
{ESC}NN{ESC}NN
{ESC}NN
½NN
§NN
where {ESC} simulate pressing the Esc key, NN are hex values and §/½ get the program to listen.
Normally we have special physical keyboard to control the program, but they are expensive. So in a test environment where we do not always have the physical keyboards, we have to enter these codes manually
You must compromise:
If you want to simulate keyboard input, then you must use SendInput, which means being at the mercy of which window currently has focus. SendInput is like hitting the keys on your physical keyboard. The only way you can send your keystrokes to a specific window using your keyboard is to ALT+TAB to the right window.
If you want to send keystrokes to a specific window, then you incur funky behavior:
Applications handle input differently. And simple WM_KEYDOWN / WM_KEYUP messages are not the only way to detect keyboard input. For example there is also the keyboard state (GetKeyboardState()) which you will have a harder time simulating. This is most likely what you're experiencing.
Applications may RELY on the standard behavior of having focus while receiving keyboard input messages. By posting messages to these applications, you invoke strange out-of-order behavior that may crash them.
Now multiple windows on the system can be receiving keyboard input at the same time. This might also cause strange behavior.
(etc...) Hooks won't be called for this input, your keyboard / input drivers won't see it, it won't be recognized by things like DirectInput... basically it's a never-ending patchwork of issues by doing something the bad-bear way.
There is no way around those side-effects; it's the consequence of doing shady stuff.
A solution for your purposes, because you're targeting a single specific application, may be to use PostMessage in conjunction with SetKeyboardState to simulate the keyboard state including shift positions.
Okay, I think you're in for a mess here, PostMessage() is notorious for not working well with shift states, and hooks won't get called either. Microsoft recommends SendInput() instead, and so do I. I suggest that you either post a new question, or update this one, where you detail what you are trying to achieve, and maybe we can better recommend a different solution.
As for the extended part, it has nothing to do with this at all, and won't help you.
What you could try, is sending a WM_KEYDOWN message that says the shift key was pressed, and then send another message with your desired key, before sending a WM_KEYUP shift message. I doubt this will work, but you can always try.
Personally i would use SendKey.Send( ) for this purpose.
MSDN page
Both Console.Read() and Console.ReadKey() seem to wait for a key to be pressed. How can I detect if anything was pressed without actually asking for the program to wait till a key is pressed?
You can poll on Console.KeyAvailable to know if you can read anything.
You want to look into using Event Handlers. For using Windows forms the following should be helpful. Control.Keypress Event (System.Windows.Forms). For a good overview of Event Handlers in general, take a look at EventHandling in .NET using C#.
For a console application, you should look into the Console.CancelKeyPress Event function.
Just add the following where you want to wait:
while (!Console.KeyAvailable) {}