I'm trying to automate the insertion of some data in a software. This software has a popup window that require a password with a textfield and 2 button.
My intent is to write that password and press ENTER on that field with a powershell script.
I've tried many methods, like Windows.Form.SendKeys, but none of that write in that field. I've even tried the InputSimulator class, but even that cannot write in that field.
The only thing I've tried that can write to that field is the On Screen Keyboard of window.
After some search I've found that it should use the SendInput command to simulate the key. Using Api Monitoring I've checked the call, and I've recreated a similar SendInput call to write a lowercase 'a', using a code similar to the one in this answer:
https://stackoverflow.com/a/18854441/4417646
I've tried with and without the wVk value, but my script still cannot write in that field.
I've monitored all the other call regarding the keyboard that is done form the onscreen keyboard, but I haven't seen any.
Before send the SendInput command, I use a code to focus the window and 1 second of sleep to be sure that the form is on focus.
If I use the same call with any other window, it work... (I've tried with notepad and other random software)
Am I missing something?
Here's the part of code that call SendInput: (it's a c# class exported to powershell)
public static void SendKeyboard(char ch) {
lpInput[] KeyInputs = new lpInput[1];
lpInput KeyInput = new lpInput();
// Generic Keyboard Event
KeyInput.type = InputType.INPUT_KEYBOARD;
KeyInput.Data.ki.time = 0;
KeyInput.Data.ki.dwExtraInfo = UIntPtr.Zero;
short key = VkKeyScan(ch);
uint mappedKey = MapVirtualKey( LoByte( key ), 0 );
// Push the correct key
KeyInput.Data.ki.wVk = 0;
KeyInput.Data.ki.wScan = (Int16)mappedKey;
KeyInput.Data.ki.dwFlags = KEYEVENTF.SCANCODE;
KeyInputs[0] = KeyInput;
SendInput(1, KeyInputs, lpInput.Size);
// Release the key
KeyInput.Data.ki.dwFlags = KEYEVENTF.SCANCODE | KEYEVENTF.KEYUP;
KeyInputs[0] = KeyInput;
SendInput(1, KeyInputs, lpInput.Size);
return;
}
Thanks to everyone!
I Hope someone can help me ;)
Related
CefSharp 73.1.120-pre01 (older version also affected)
x64, Win10, WinForms
I use CefSharp.WinForm to "host" simple Angular app with few input controls. Because of need to map some keyboard keys to some another I use IKeyboardHandler (OnPreKeyEvent).
When I return "true" from OnPreKeyEvent (bool) I am able to suppress initially pressed key BUT instead of returning just one "transformed" value he returns duplicated new value to web input control.
Simple use case
I press "a" in input control
2.
MyKeyHandler which implement IKeyHandler (OnPreKeyEvent) method enter his codebehind
For sending new key I used KeySend(Sys.Win.Form.SendKeys):
KeySend.SendWait("{s}")
or InputSimulatorPro Nuget package:
InputSimulator sim = new InputSimulator();
sim.Keyboard.KeyDown(WindowsInput.Native.VirtualKeyCode.VK_S);
3.
Web Input control receive 2 values od "s" character.
I tried SendKey.Flush / Thread.Sleep combinations but nothing has impact - I always get 2 values OUT.
Sample code (End -> Tab) <- Returns 2 tabs (it shift one web control more than it should)
public bool OnPreKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey, ref bool isKeyboardShortcut)
{
if (windowsKeyCode == 0x23) //End > Tab
{
SendKey.SendWait("{TAB}");
return true;
}
ANY help would be greatly appreciated.
I thought that code is pretty good, bu it isn't. I'm trying to add something text to my notepad, which look like:
string text = "TESTTESTTESTTEST";
[DllImport("user32.dll")]
private static extern int SetForegroundWindow(IntPtr hWnd);
public void EditTxtFile(string text)
{
Process p = Process.GetProcessesByName("notepad").FirstOrDefault();
if (p != null)
{
IntPtr handle = p.MainWindowHandle;
SetForegroundWindow(handle);
SendKeys.SendWait(text);
p.Kill(); //also process doesn't shoutdown
}
}
When i try to debug this function (actually SendKeys), that message is showing up:
Changes are not allowed while code is runnig.
If it's important i try to edit this from web page/application.
The problem is, that Visual Studio catches Focus when it hits the break point, and then SendKeys is send to Visual Studio instead of notepad, and this produces the error.
What you can do ist the following:
Right Click on the Breakpoint and select "When Hit..."
There you can output whatever you want without Notepad losing Focus
Problem was in function which was responsible for opening that notepad (not enough time to start and open him). I've just add wait function for 4s.
To mine, it's about I use a reference value text from the source WinForms's control, call SendKeys' function while the source form is not yet close or hide before (still the active form). This can produce a similar result: SendKeys.Send() or SendKeys.SendWait() does not send a desired text to the target application.
The steps to workaround this:
1. transfer the text value from the active form to a new temporary variation or a Clipboard,
2. close or hide the active form,
3. activate the target application's form,
4. wait a bit to ensure the target application's form to become active 5. and give the temporary text variation or Clipboard to SendKeys. Works.
PS: Please make sure your application has runtime permission equals or more than the target application.
I have a very basic problem, For a game (Dota 2) I want to write a little macro which opens the console, writes "setinfo name ".........................."" into it and then closes it, the hotkey for console is set to '#'. For this I wrote an application which listens for the key 'f' to be pressed, and then send
1) '#' (open the console)
2) "messag ebla bla b...."
3) '#' (close the console)
everything is working except that it will not open the console (but if the console is already open it will write #messagej.j....# into it when i press f just as wanted)
my code for sending the keys.
SendKeys.Send("#");
SendKeys.Send("my message for consol");
SendKeys.Send("#");
does anybody know why the hotkeys dont work by sending keys? I thought its an simulation of when the user presses F or Q.
First thing You should try is adding a "software" delay between keypresses.
string MSG = "#yourmessage#";
foreach (object c_loopVariable in MSG) {
c = c_loopVariable;
Sendkeys.Send(c);
sleep(20);
}
The other thing to consider - sendkeys do not emulate the keypress at the hardware layer - it is on windows API level, and the DirectX DirectInput., witch most games use for user input processing goes a level further. So it is possible You will not be able to do it this easy.
You should check out more of keybd_event. Ive had better luck using that - as i understand it goes a level deeper than just sendkeys.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646304(v=vs.85).aspx
Anyways, I hope u are not going to use this for some spamming in a great game! :)
I'm trying to simulate a keypress into another window, which is a game. The things that don't work are:
SendKey - Send the input as strings like some text or something but doesn't go for ENTER TAB or other critical keys
InputSimulator - Complete waste of time
SendInput - Have some problems with this as well. It doesn't compile, and I've never used it before
PostMessage - it seems these 2 are pretty much the same and again they do send text but again no other keys such as ENTER TAB or other. Also couldn't figure out the value for wParam if i wanna hold down the key for a number of seconds. It is a 32bit Hex value in which seems the last 16 are repetition number.
SendMessage - See PostMessage
AUTOIT or AHK it seems that they have troubles being incorporated into C# if anyone knows how to do that, it would help immensely, this meaning I'd be able to write AHK script in the C# .cs file so it would compile. COM, ref, or anything.
Having problems with DirectInput
public void Test_KeyDown()
{
INPUT[] InputData = new INPUT[2];
Key ScanCode = Microsoft.DirectX.DirectInput.Key.W;
InputData[0].type = 1; //INPUT_KEYBOARD
InputData[0].ki.wScan = (short)VirtualKeyCode.NUMPAD2 ; //ScanCode;
InputData[0].ki.dwFlags = (int)KEYEVENTF.SCANCODE;
InputData[1].type = 1; //INPUT_KEYBOARD
InputData[1].ki.wScan = (short)VKeys.VK_W;
InputData[1].ki.dwFlags = (int)(KEYEVENTF.KEYUP | KEYEVENTF.UNICODE);
// send keydown
if (SendInput(2, InputData, Marshal.SizeOf(InputData[1])) == 0)
{
System.Diagnostics.Debug.WriteLine("SendInput failed with code: " +
Marshal.GetLastWin32Error().ToString());
}
}
It gives this error:
Could not load file or assembly 'Microsoft.DirectX.DirectInput.dll' or one of
its dependencies. is not a valid Win32 application.
(Exception from HRESULT: 0x800700C1)
I've referenced it from DirectX SDK tool kit 2007
I'm trying to send a key to another instance or handle. C++ or C# anything that works is fine, too.
Looks like you are trying to load a 32bit dll in a 64 bit solution, so please use the 64bit dll
I'm writing an app to control an existing instance of WMP using C#.
Currently my code goes something like this:
private const int WM_COMMAND = 0x111;
private const int WMP9_PLAY = 0x4978;
SendMessage(WMP.MainWindowHandle, WM_COMMAND, WMP9_PLAY, 0);
This works fine for pausing media player, but if media player is paused or stopped, it skips to the next track. The command is called play, but I could have the wrong value for it. Does anyone either have a better value for WMP9_PLAY, or a better way to make WMP play?
By using Spy++ (from the answer above) I was able to find another set of SendMessage parameters that is sent when you press the play/pause on a windows keyboard that does the job:
SendMessage(WMP.MainWindowHandle, 0xC02B, 0x0000000C, 0x000E0000);
Check out the sample code here: Interoperating with Windows Media Player using P/Invoke and C#