Disable keys on the default Android keyboard - c#

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.

Related

Write the string name of the Input key

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.

UWP - InjectedInputKeyboardInfo how to send non-English keystrokes

I have a UWP app in Visual Studio 2017. I'm trying to make a multi-language on-screen keyboard.
Currently the English keystrokes are working fine, however any letter from other languages throws System.ArgumentException: 'Value does not fall within the expected range.'
Here is the code that sends the keystrokes:
public void SendKey(ushort keyCode)
{
List<InjectedInputKeyboardInfo> inputs = new List<InjectedInputKeyboardInfo>();
InjectedInputKeyboardInfo myinput = new InjectedInputKeyboardInfo();
myinput.VirtualKey = keyCode;
inputs.Add(myinput);
var injector = InputInjector.TryCreate();
WebViewDemo.Focus(FocusState.Keyboard);
injector.InjectKeyboardInput(inputs); // exception throws here
}
How would I inject letters from other languages?
The trick is that InputInjector isn't injecting text (characters), but actually it is injecting key strokes on the keyboard. That means the input will be not what the VirtualKey value contains as the name value, but what the given key represents on the keyboard the user is currently using.
For example in Czech language we use the top numeric row to write characters like "ě", "š" and so on. So when you press number 3 on the keyboard, Czech keyboard writes "š".
If I use your code with Number3 value:
SendKey( (ushort)VirtualKey.Number3 );
I get "š" as the output. The same thing holds for Japanese for example where VirtualKey.A will actually map to ”あ”.
That makes InputInjector for keyboard a bit inconvenient to use, because you cannot predict which language the user is actually using a which keyboard key mapping is taking place, but after reflection it makes sense it is implemented this way, because it is not injection of text, but simulation of actual keyboard keystrokes.
The answer given by Martin Zikmund is not true. You can send any unicode character.
InputInjector inputInjector = InputInjector.TryCreate();
var key = new InjectedInputKeyboardInfo();
key.ScanCode = (ushort)'Ä';
key.KeyOptions = InjectedInputKeyOptions.Unicode;
inputInjector.InjectKeyboardInput(new[] { key });
The InjectKeyboardInput method is using this function behind the scenes. Please note the that you require the inputInjectionBrokered capability in your app.

Translating Windows.forms.Keys to real local keyboard value

I use the namespace Windows.Forms.Keys
I would like to be able to catch and use some special like characters like é,è,à,ç, but when the program fire the event KeyDown, the KeyEventArg just return me the value "D1" to "D9".
What could I do to get the real char associated to these keys ?
Short answer: use KeyPress instead of KeyDown.
KeyDown is really designed to work with the physical layout of the keyboard (well, the logical layout of the physica... forget it :D). This is very different from the character that given physical key represents.
On the other hand KeyPress is all about characters being input from the keyboard, rather than keys being pressed, really. Note how KeyPress supports features like AltGr + someKey and char repetition etc.
If you really need to use KeyDown/KeyUp, you'll have to emulate the way windows keyboard system works to determine the char to output (for example, if you're making a keyboard mapping screen for a game or something like that). You can use the ToAscii WinAPI method (https://msdn.microsoft.com/en-us/library/ms646316.aspx).
Apart from that, you still have to understand the meaning of the key combinations - for example, on my keyboard, if I press 1, I get +. If I press Shift+1, I get 1. If I press AltGr + 1, I get !. Which of those do you care about? Maybe Shift + 1 should be interpreted as 1 (what KeyPress does). Maybe it should be interpreted as Shift + 1 (the easiest, you already have that). And maybe it should be interpreted as Shift + +, the way it's usually used for hotkey bindings or keyboard mappings in games.
It should be pretty obvious by now that this is actually far from trivial. You need some mechanism to interpret the "raw" input data - and different interpretations make different sense for different initial conditions. You're basically asking for a mixed approach between the two obvious options - you're mixing virtual keys and "real" characters.

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.

How simulate CTRL+V keystrokes (paste) using C#

How can we simulate CTRL+V keys (paste) using C#?
I have a textbox that hasn't a id for access, for example textbox1.Text = someValue won't work here.
I want to fill that textbox (from clipboard) by clicking on it. For some reasons we exactly need simulate CTRL+V, mean we cannot use external libraries like inputsimulator.
Character vs key
% => alt , + => shift and ^ is used for ctrl key
Original Answer:
Simulation of single modifier key with another key is explained below
Step1: Focus the textBox, on which you want to perform two keys and then Step2: send the key for example control-v will be sent like "^{v}". Here is the code
target_textBox.Focus();
SendKeys.Send("^{v}");
target_textBox.Focus(); is needed only when target textbox is not focused at the time of sending key
Update: For sending three keys (two modifying keys plus other key) like to achieve ctrl shift F1 you will send following
^+{F1}
Microsoft Docs Ref
Why don't you override the TextBox OnClick event than when the event is called, set the Text property to Clipboard.GetText()
Like:
private void textBox1_Click ( object sender, EventArgs e )
{
textBox1.Text = Clipboard.GetText ();
}
This function is already built in: TextBoxBase.Paste()
textbox1.Paste();
some JS do not permit to change value in usual way
inputList[21].SetAttribute("value", txtEMail.Text);
you should try something like this:
inputElement.InvokeMember("focus");
inputElement.InvokeMember("click"); //sometimes helpfull
Clipboard.SetDataObject(txtEMail.Text);
SendKeys.Send("^(v)");
//but not "^{v}"
In case the language in the operating system is not English, this option may not work:
SendKeys.Send("^{v}");
Then try this option:
SendKeys.Send("+{INSERT}");

Categories