C#: Check if any key was pressed in Console - c#

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) {}

Related

C# - Detecting Keydown and KeyUp events in the background

I’ve been trying to create an console application which runs in the background detecting any key ups and keydown events, I’ve seen some threads on global key hooks however, I’m unable to make it detect key ups and key downs rather then key presses.
I’d like some advice on how to go about it, any help is valued and appreciated, thank you.
Figured out a solution using GetAsyncKeyState()

MonoDevelop - Detect Enter Keypress in TextBox

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!

Listening for input on a console app in a separate thread

I am currently working on a c# project which has an event handler for Console.CancelKey EventHandler. The problem I am having is if the user presses Ctrl+C to stop the app the event doesn't get triggered as I think it is too busy doing other stuff to handle the event. If the app is quiet, i.e. not having to do much work the Ctrl+C is caught and it runs that event handler.
I assume I need to listen for the event handler in a separate thread so that it be understood but how would I go about doing this.
Thanks for any help you can provide.
You might try setting Console.TreatControlCAsInput, which stops CTRL+C combination from even being processed at the OS-level. The caveat being that if the user truly does want to stop the application, they will either have to use CTRL+BREAK, or you will have to manually force the application to close after running your event handler.

keyboard press without Hardware press

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

How can I listen in on shortcuts when the app is the task bar in C#

An example of an app that does this is Enso, it pops up when you press the caps lock.
You can act on global hotkeys by calling the winapi function RegisterHotKey. Also see https://www.codeproject.com/Articles/4345/NET-system-wide-hotkey-component and https://www.codeproject.com/Articles/3055/System-Hotkey-Component for example. You can not use all key combination as hotkeys. For those that don't work you might try a global keyboard hook (SetWindowsHookEx)
You need to install a hook in user32.dll. Lookup the Win32-API call SetWindowsHookEx. You can call it from C# via the stuff in System.Runtime.InteropServices.
This article discusses the topic nicely.
Edit: Lars Truijens answer looks like a nicer approach actually.

Categories

Resources