Detecting key in C# console, without "using" it - c#

I am trying to make a simple menu.
My question is simple: Is there a way to detect if key is pressed directly from the keyboard? In such way, that even if the user has started typing something, if he presses "~" it would be detected and some action will follow?
I know that is not possible in the console. I could only use Console.ReadKey() but that accepts the whole character, and if its not '`' it is discarded. Possible fix could be to add that key to the string later, but that would require the first key to be "~" which is annoying.
That is why I need something to detect the keys as they are pressed. I hope you can help.
Thanks.

Related

Unity keyboard control (press on a key from C# script)

How can I press a key from the C# script? All I found it's just how to get click on a key, I did not find how to press programmatically on a key. How can I do that?
I want to make the key pressed. Not to check if the key is pressed.
For example, I want to press Enter with my C# script.
There is no way in vanilla Unity to simulate a keypress, I believe. Based on what you require, it's most likely that you're trying to simulate an event happening. What you could do is simply make a function that's going to execute the event that's going to happen when you press ENTER and then call that event whenever it's supposed to be executed.
It's already been discussed here :
How to simulate a key press on button click - Unity

Listening for ESC press during console application without 'stealing' first character of user input prompt

I'm practicing with C# on my road to become a developer, so I'm kinda new with all this.
I'm writing a simple console app to get some practice mileage, using the challenges I run into to learn more about how to work with the language. Because I love D&D I made a simple app that rolls different dice with pseudorandom numbers, with the possibility of adding modifiers. It asks the user which kind of die he/she would like to roll, how many, if there's any modifiers etc. etc.
In short, here's my problem: I want to give the user the possibility to exit the program at any given time by pressing ESC, while otherwise functioning as normal and handling user input as normal.
My console app is dependent on user input. All it does is ask the user for input, and then do something with that input, like a calculator. There's at least 6+ different moments where the user is prompted for input (type of die, amount of dice, adding modifiers etc.). What I want to do is offer the user the possibility of exiting the program at any time by pressing ESC instead of inputting their data. Because there are so many prompts, I want to prevent adding a check for ESC at every prompt so the code doesn't get repetitive and clunky.
I tried multithreading with a listener for the ESC key, but I found that it 'stole' the first character of every user input prompt. If I press ESC, the program neatly exits just like I want to, but if I don't want to exit and just input my data, the first keystroke is missing because it was used to check for the ESC key.
Is there a way to check if it's the ESC key, and if it isn't, just keep the input where it was?

How to get global keyboard input in UWP app without using CoreWindow.KeyDown

Since CoreWindow.KeyDown is broken in the latest few builds (6 months) of windows 10 I need an alternative way to get keyboard input. There are 3 ways of getting mouse input that I know of but I cannot find any alternative ways for keyboard input.
Are there any other methods for keyboard input in a UWP program? If so, what are they?
The issue is that CoreWindow.KeyDown events can be delayed by up to 30 seconds!
Daniel, there is one alternative way: CoreWindow.Dispatcher.AcceleratorKeyActivated
But beware: It gives false positives e.g. when I press the Menu key (Alt) it also says that I'm pressing the Control key.
Also it makes a difference between KeyDown and SystemKeyDown (I couldn't find a definition of the difference between the two), but when trying to pair the with KeyUp and SystemKeyUp you're also in for a surprise as they do not always match. :/
Anyway, if you get it to work for you then CoreWindow.Dispatcher.AcceleratorKeyActivated is the answer to your question.

Robust SendKeys solution required

I'm trying to write an application that can reliably send key presses to another application.
The second app has a text field which opens when 9 is pressed.
Text can then be typed and then the text field is closed when Enter is pressed.
If I use SendKeys to try to do this the field does not open if I send 9, but if the field is already open my tool can send text ok, but then it does not close if I send an Enter.
I'm guessing this is because a lower-level interaction with the keyboard driver or similar is being used.
Is there a reliable way I can simulate actual keyboard input to the application from another C# app?
I have found a few potential solutions online but these are generally incomplete with either missing references etc. or missing code elsewhere.
I will continue to search and I will post the solution here if I find it!
is it an option to invoke on screen keyboard provided by os. your app, calling app will loose or cant track the key events, however if you open your second application and invove osk.exe, then the purpose of robust keyboard sending keys to an app could be fullfilled.
I explained this here
EDIT: ignoring on screen keyboard
You can try using RegisterHotKey to open the textpad on a specific sequence of key(s) and persist the handle in a instance variable. On the sequence of second hot key(s) you can close the textpad (you already know the handle)
there is a video explaining this

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!

Categories

Resources