Write the string name of the Input key - c#

In my game, the user can set a key to open the game-console.
I want to show an info during the game to the user, that they can open the console by pressing the key, they've set before.
For example:
The default key for the console is f1. Then it should show:
Press F1 to open Console
If the user sets the C key, it should write:
Press C to open Console
But i don't find any way to write down the key, the user set before, by code.
Edit:
I'm sorry that it wasn't that clear what i mean.
I added a screenshot of the Input-Configuration (which is the default Unity Input-Configuration).
In this Configuration the user can set a key for OpenConsole by double clicking on f1 (in the Primary row).
In Unity i can check if a specific Button is pressed like this:
if (Input.GetButtonDown("OpenConsole"))
{
...
}
But what i want is, that i can show the user which key they have chosen for OpenConsole. Something like this:
text.text = "Press the " + WhateverTheUserSet + " Key to open the Console!";

Okay, your question isn't clear as to how the default / redefined key is set / changed by the user but I'll give two solutions based on two scenarios:
Scenario 1: Assumes you are asking the user what key to use via an onscreen "Redefine Key" page.
In this situation, you will probably use something along the lines of Console.ReadKey() where you are capturing the user's keyboard press. You end up with a key code which could be one of around 100 keys on a keyboard. You will need to store internally a mapping between keycode and "text". If they user had pressed F1 and this is keycode (say 232) then you will use the mapping to give you the string "F1" which is what you display on screen. You would store the keycode in config for use in the game and for peristance between launches of the game.
Scenario 2: Assumes the user edits a config file and sets the value in there.
In this situation, you need the mapping going the other way. If the config file includes (by default):
ConsoleKey=F1
Then you need a similar internally stored mapping but in the other direction. If the user changes the value to "A" then this would need to map to a keycode (e.g. 65). The user can change the config file and the mapping will tell the user what key to detect in game. You can display the same text as in the config file for your on-screen "info".
If you can be clearer in how you are intending to implement this then we can provide a clearer answer.

Unfortunately, Unity doesn't have a way to check which keys are assigned from the input manager. If you need more control, you'd need to implement your own key mapping solution.

Related

How do I get Input to read other keys and not just Up and Down

I saw some codes like this:
if (Input.GetKey(KeyCode.UpArrow))
and
if (Input.GetKey(KeyCode.UpArrow))
Wanted to know how do I put other keys, as in the case of the "a"
if (Input.GetKey(KeyCode.AArrow))
tried this code here however it gives an error, as I write?
When using KeyCode in comparison (like an if statement) you must call the specific keycode you want to compare the input to. Microsoft has documentation which provides the standard KeyCodes represented in System.Windows which most, if not all, external libraries call to.
So in this case you need to check for the A key specifically.
if (Input.GeyKey(KeyCode.A))
{
//code runs if and only if the input key is the A key
}
else if (Input.GetKey(KeyCode.Escape))
{
//code runs if and only if the input key is the ESC key.
}
Now if you want many possible inputs at a specific time you may not wish to use an if statement, but I would recommend trying this out first to see if you get better results.

C# Check for key press without delay (Console application) [duplicate]

This question already has answers here:
Listen on ESC while reading Console line
(6 answers)
Closed 1 year ago.
first time posting here, so I apologise in advance in case I miss something important, please let me know if that's the case!
I'm writing a console application that has a menu with several different options in different layers, and I'm trying to make a function that will, at any time as the user works in these menus, notice if the Esc key is pressed. To make my code less cluttered I wrote a separate method that I can simply call at any point when I'm asking for input (like a choice, or entering information into a form, or such).
The problem I have is that it always cancels out the first key press. If a user enters a number to move in the menu, the number doesn't get entered, if they enter a name into a form the first letter is missing etc.
Is there a way around this?
The code currently looks like this;
public static void checkForEsc()
{
ConsoleKeyInfo key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Escape)
{
switch (currentUserisAdmin)
{
case true:
Menu.AdminMenu();
break;
case false:
Menu.CustomerMenu();
break;
}
}
}
Thanks in advance :)
Edit:
Might be worth adding that the code where this snippet gets called looks something like this, with very small variations;
Console.WriteLine($"1. Add a user\n2. Remove a user \n3. See user info \n \n9. Cancel");
Program.checkForEsc();
int response2 = CheckIfResponseInt(Console.ReadLine());
You need to write your own line editor instead of using Readline. It's described in the link below. You may need to add support for other control keys such as backspace or add a check that only numeric keys are being pressed.
Listen on ESC while reading Console line

Disable keys on the default Android keyboard

I've got an Entry control on my Xamarin application home page, which is used for entering a person's age. I've set the keyboard to be Keyboard="Numberic"
However, this is causing confusion as, for Android at least, the "Done" key is below backspace, but above the settings key as well as the .- key. This means when the user is trying to press "Done", they're forgetting that the key isn't in the bottom right-hand corner as you'd expect, and they keep pressing the settings key by mistake and going into their phone settings, which is a bit irritating, understandably.
Is it possible to either disable the settings key and the .- key, or swap their positions around? I'm not expecting it to be possible, so in which case, is there another way that I can get around this?
Screenshot to clarify what I mean - can the keys with the settings cog and the ".-" be moved or disabled, to prevent the user opening their phone settings and putting negative/decimal numbers in?
I have not used C# in a bit and never with Android but the idea of removing characters from a String is most languages is normally simple enough.
Based on our small discussion in the comments, and a little research; I believe this is what you are looking for to strip unwanted values from the EditText field where the user's input goes in your application:
var editText = FindViewById<EditText> (Resource.Id.editText);
editText.TextChanged += (object sender, Android.Text.TextChangedEventArgs et) => {
//step 1 grab editText value
String newString = et.Text.ToString();
//step 2 replace unwanted characters (currently '.' & '-')
newString = newString.Replace(".", "").Replace("-", "");
//step 3 set the editText field to the updated string
editText.Text = newString;
};
Resource:
https://github.com/xamarin/recipes/tree/master/Recipes/android/controls/edittext/capture_user_input_text
I have not found a way to change the default keyboard for Android so you can remove unwanted keys (i.e. the settings key) or reorder them, so I am still thinking if you decide that you must do this, you would need to build a custom keyboard for the application.

C# Sendkeys class {SHIFT}

I am trying to use the Sendkeys class to send a SHIFT key. But it doesn't allow for SHIFT. Only BACKSPACE or ENTER or basically anything but SHIFT. I need a way to send shift keys, like, Sendkeys.Send({SHIFT});
Is there a way to do this? Is there another way to send SHIFTs?
%() sends just the Alt command. So I assume that +() will send just the shift key. However I cannot think of a way to test this, since the shift key does nothing by itself. pressing shift 5 times normally prompts if you want to turn on sticky keys, but +()+()+()+()+() does not prompt for sticky keys.
Read the senction just after it explains how to send F1 to F12: https://msdn.microsoft.com/en-us/library/office/gg278655.aspx
%A = Alt+A
%(AE) = Alt+A+E
%()AE = ALT,A,E

vb.net or c#.net console.readkey shift+number chars

I've been having some trouble getting the correct code for incoming keystrokes on the console for shift+number characters. For example, using:
cki = Console.ReadKey(True)
Console.WriteLine("You pressed the '{0}' key.", cki.Key)
If I press shift+2, I'm hoping to get the ascii 64 (for the '#' character), but instead I get 50 (for the '2' character).
Now, I know you can get the modifiers for the key pressed, but that would mean I'd have to program all the special cases for keys like that, and that doesn't seem right.
I need this function, or something like unto it, because of its ability to read keys as they are pressed, without the need to press enter, otherwise I'd just use console.read. Surely I've missed something. Could anyone tell me what it is I've missed?
You're looking for the KeyChar property, which returns the actual character rather than the physical key pressed.
You may want to cast it to int.
It is pretty important to distinguish between keys and characters. The key is the same anywhere in the world, the one on the top row at the left. You can rely on that key always producing ConsoleKey.D2
The character is however very different, it greatly depends on the active keyboard layout. A Northern American user presses Shift+2. A French user presses AltGr+0. A German user presses AltGr+Q. A Spanish user presses AltGr+2. Etcetera.
If you care only about the key then use ConsoleKeyInfo.Key, you do so for all non-typing keys like the function keys for example. Perhaps the typical gaming WASD keys. If you care only about the character, like #, then use ConsoleKeyInfo.KeyChar.

Categories