How simulate CTRL+V keystrokes (paste) using C# - 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}");

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.

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

Keydown Eventhandler Process

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.

Sending Windows key using SendKeys

I am working on shortcuts in C#. I succeed implementing Ctrl, Alt and Shift with SendKeys.
Like this;
Ctrl + C:
System.Windows.Forms.SendKeys.SendWait("^c");
or Alt + F4:
System.Windows.Forms.SendKeys.SendWait("%{F4}");
But I can't send "Windows Key" with SendKeys. I tried ex: Win + E : .SendWait("#e") but it's not working. What should I use instead of "#"?
Thanks.
OK turns out what you really want is this: http://inputsimulator.codeplex.com/
Which has done all the hard work of exposing the Win32 SendInput methods to C#. This allows you to directly send the windows key. This is tested and works:
InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.LWIN, VirtualKeyCode.VK_E);
Note however that in some cases you want to specifically send the key to the application (such as ALT+F4), in which case use the Form library method. In others, you want to send it to the OS in general, use the above.
Old
Keeping this here for reference, it will not work in all operating systems, and will not always behave how you want. Note that you're trying to send these key strokes to the app, and the OS usually intercepts them early. In the case of Windows 7 and Vista, too early (before the E is sent).
SendWait("^({ESC}E)") or Send("^({ESC}E)")
Note from here: http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx
To specify that any combination of SHIFT, CTRL, and ALT should be held
down while several other keys are pressed, enclose the code for those
keys in parentheses. For example, to specify to hold down SHIFT while
E and C are pressed, use "+(EC)". To specify to hold down SHIFT while
E is pressed, followed by C without SHIFT, use "+EC".
Note that since you want ESC and (say) E pressed at the same time, you need to enclose them in brackets.
download InputSimulator from nuget package.
then write this:
var simu = new InputSimulator();
simu.Keyboard.ModifiedKeyStroke(VirtualKeyCode.LWIN, VirtualKeyCode.VK_E);
in my case to create new vertial desktop, 3 keys needed and code like this(windows key + ctrl + D):
simu.Keyboard.ModifiedKeyStroke(new[] { VirtualKeyCode.LWIN, VirtualKeyCode.CONTROL }, VirtualKeyCode.VK_D);
Alt+F4 is working only in brackets
SendKeys.SendWait("(%{F4})");
SetForegroundWindow( /* window to gain focus */ );
SendKeys.SendWait("^{ESC}"); // ^{ESC} is code for ctrl + esc which mimics the windows key.
For sending combination of Ctrl+Alt+Right ==> use this ==> "(^%({RIGHT}))"

Categories