I am trying to process to keydown event but the below code is process only first char of string and not able to remove second char of string.
private void Game_KeyDown(object sender, KeyEventArgs e)
{
if(BirdsArray[SelectedIndex].THIS_STRING.First()== (char)e.KeyData)
{
BirdsArray[SelectedIndex].THIS_STRING = BirdsArray[SelectedIndex].THIS_STRING.Remove(0, 1);
this.Validate();
}
}
In this below code BirdsArray is array of panels, THIS_STRING is a string type of string object in BirdsArray.
The KeyDown event gives you the virtual key code of the key that was pressed. Like Keys.A if you press the A-key. It just tells you about the key, not about the letter that's produced by the key. Which depends on the active keyboard layout (it is not an A in Japan or Russia for example) and what modifier keys are down. Shift being the obvious example, producing either A or a. Or Alt which, when down, doesn't produce a letter at all.
So your code will work by accident if the first letter of the string is capitalized. You'll run out of luck when the rest are not. A does not match a.
It is pretty unclear to me why you need this feature. Do consider the KeyPress event instead. Which gives you the actual letter that the user sees on the screen.
Related
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.
I'm writing a VNC client for HoloLens using C# and I'm having a tough time figuring out how to handle keyboard input. KeyUp/KeyDown give me a Windows.System.VirtualKey object, but there doesn't appear to be an API to map these VirtualKeys (along with modifiers, e.g. shift) to the characters they represent on a given layout. E.g. VirtualKey.Shift + VirtualKey.F == 'F' versus 'f' when it's simply VirtualKey.F. Or Shift + 5 to give % on a US keyboard.
In win32 apps you'd use MapVirtualKey to handle the keyboard layout for you -- how does this get handled in UWP?
It is not possible to get the translated character in KeyUp/KeyDown events. But it is possible when using CoreWindow.CharacterReceived event to get the translated character.
You can register the event by the following codes:
Window.Current.CoreWindow.CharacterReceived += CoreWindow_CharacterReceived;
And you will get a KeyCode of the translated input character(e.g. for shift+5 it gets 37, while for 5 it gets 53) through the CharacterReceivedEventArgs:
private void CoreWindow_CharacterReceived(CoreWindow sender, CharacterReceivedEventArgs args)
{
uint keyCode=args.KeyCode;
}
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 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}");
I am currently developing in XNA/C#.
When the user presses a key (Keys.Right), I need to move an object.
I want this to happen
when the user presses the key
after 1 second while the user is holding the key and then every .25 seconds.
I already implemented the first one:
_kbOld = _kbNew;
_kbNew = _kb.GetState();
if(_kbNew.IsKeyDown(Keys.Right) &&
_kbOld.IsKeyUp(Keys.Right))
{
//Do something
}
How would I do the other actions?
I had the following ideas:
A Queue<KeyboardState>, keeping track of the last KeyboardStates
Saving the time the key was last pressed and when it was released (GameTime)
It should work like text input in Windows: When you hold a letter, it will repeat after a certain amount of time.
Which way should I use? Do you have other ideas?
Thanks in advance!
I would simply store the last push time, like you suggested:
if (IsPressed())
{
// Key has just been pushed
if (!WasPressed())
{
// Store the time
pushTime = GetCurrentTime();
// Execute the action once immediately
// like a letter being printed when the button is pressed
Action();
}
// Enough time has passed since the last push time
if (HasPassedDelay())
{
Action();
}
}
I recall from doing something similar in javascript that
you create a timer, set its interval to 1 second, and then enable it on mousedown (keydown in your case). You will reset and disable it on keyup.
The second step is to tie your behaviour to the the Tick() event.
The third step is to check, inside your Tick() handler, if the state of Keys.Right is down and if not, to reset the timer.
I suggest you to make your own Key and Keyboard class. In Keyboard you could make method updateInput() which would update state of every Key, where you could use advenced functions like this.
In Key class, you can use
Saving the time the key was last pressed and when it was released
(GameTime)
#JuannStrauss
For sure not timer.