How to use InputSimulator to simulate specific keys on Remote Desktop? - c#

Im currently using the InputSimulator v0.1.0.0 to simulate keypresses and/or mouse events over Remote Desktop. Basic keypresses (for example pressing 'a') works, but special characters, like 'tab', 'enter' dont.
I simulate entering texts with:
InputSimulator.SimulateTextEntry("blabla");
but the following only works locally:
InputSimulator.SimulateKeyPress(VirtualKeyCode.TAB);
or
InputSimulator.SimulateKeyPress(VirtualKeyCode.RETURN);
I searched over the net for working examples but i havent found anything useful. Anyone has idea how to make it work?
Thanks in advance!
-------------------------OWN ANSWER----------------------------------
After googling some more, i have found this article:
http://www.pinvoke.net/default.aspx/user32.keybd_event
in which there is a good code, that does not solve the InputSimulator problem, but does exactly that i need. Here is the code, and how i used that:
[StructLayout(LayoutKind.Sequential)]
public struct KEYBOARD_INPUT
{
public const uint Type = 1;
public ushort wVk;
public ushort wScan;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT
{
public int dx;
public int dy;
public uint mouseData;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
};
[StructLayout(LayoutKind.Explicit)]
struct KEYBDINPUT
{
[FieldOffset(0)]
public ushort wVk;
[FieldOffset(2)]
public ushort wScan;
[FieldOffset(4)]
public uint dwFlags;
[FieldOffset(8)]
public uint time;
[FieldOffset(12)]
public IntPtr dwExtraInfo;
};
[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
public uint uMsg;
public ushort wParamL;
public ushort wParamH;
};
[StructLayout(LayoutKind.Explicit)]
struct INPUT
{
[FieldOffset(0)]
public int type;
[FieldOffset(4)]
public MOUSEINPUT mi;
[FieldOffset(4)]
public KEYBDINPUT ki;
[FieldOffset(4)]
public HARDWAREINPUT hi;
};
[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, IntPtr pInput, int cbSize);
And this is how i called the 'press TAB' event:
keybd_event(0x09, 0x0f, 0, 0); // Tab Press
keybd_event(0x09, 0x0f, 0x0002, 0);

As suggested i copy my solution as an answer. I hope these will help for anyone working on similar problems. :) The solution is a bit long, but the problem was not only 'how-to-press a button programatically', but also 'how-to-make it work via remote desktop' and 'how-to-make a general solution for different keyboards'. Well, im not 100% sure that the last problem is completely solved, but the solution below may be used for further developing. I also know that the code is not optimal and sometimes ugly, but im still testing and developing it! :)
//m_text is the whole text i want to write. It may contain special characters,
//like 'enter', 'tab', lower/upper-case chars, and chars with shit/alt is
//pressed, like ';'.
//Test with this string, its difficult-enough. :)
string m_text = "123qweQWE;{tab}!{eNTer}*|";
IntPtr keyboardLayout = GetKeyboardLayout(0);
while (!string.IsNullOrWhiteSpace(m_text))
{
int m_Index = 0;
//Enter, tab and similar keys are in {} brackets
//(for example {tab}). We get the 'tab' from the
//string and pass this to our method. Key combinations
//are separated by a '+' like {alt+tab+tab}, from this
//we will get 'press the alt, then press the tab, then
//press the tab again'.
if (m_text[m_Index] == '{')
{
#region [ Special chars ]
string m_SubString = m_text.Substring(
m_Index + 1, m_text.IndexOf("}") - 1);
string[] m_Splitted = m_SubString.Split(new char[] { '+' });
for (int i = 0; i < m_Splitted.Length; i++)
{
//If the string is longer than 1 char it means we are processing a tab-like key.
if (m_Splitted[i].Length > 1)
PressSpecial(m_Splitted[i]);
else
{
//If the char is 1-char-long, it means we previously pressed a tab-like key,
//and now we press a simple key, like in the case of {altgr+w}.
//Get the virtual key of the char.
short vKey = VkKeyScanEx(
char.Parse(m_Splitted[i]), keyboardLayout);
//Get the low byte from the virtual key.
byte m_LOWBYTE = (Byte)(vKey & 0xFF);
//Get the scan code of the key.
byte sScan = (byte)MapVirtualKey(m_LOWBYTE, 0);
//Press the key.
//Key down event, as indicated by the 3rd parameter that is 0.
keybd_event(m_LOWBYTE, sScan, 0, 0);
}
}
Application.DoEvents();
//We have pressed all the keys we wanted, now release them in backward-order
//when pressing alt+tab we beed to release them in tab-alt order! The logic is the same.
for (int i = m_Splitted.Length - 1; i > -1; i--)
{
if (m_Splitted[i].Length > 1)
ReleaseSpecial(m_Splitted[i]);
else
{
short vKey = VkKeyScanEx(
char.Parse(m_Splitted[i]), keyboardLayout);
byte m_LOWBYTE = (Byte)(vKey & 0xFF);
byte sScan = (byte)MapVirtualKey(m_LOWBYTE, 0);
//Key up event, as indicated by the 3rd parameter that is 0x0002.
keybd_event(m_LOWBYTE, sScan, 0x0002, 0); //Key up
}
}
Application.DoEvents();
#endregion
//We do not use the '{' and '}' brackets, thats why the '+2'. :)
m_Index = m_SubString.Length + 2;
}
else
{
#region [ One char ]
short vKey = VkKeyScanEx(m_text[m_Index], keyboardLayout);
//Hi-byte indicates if we need to press shift, alt or other similar keys.
byte m_HIBYTE = (Byte)(vKey >> 8);
byte m_LOWBYTE = (Byte)(vKey & 0xFF);
byte sScan = (byte)MapVirtualKey(m_LOWBYTE, 0);
//Press the special key if needed.
if ((m_HIBYTE == 1))
PressShift();
else if ((m_HIBYTE == 2))
PressControl();
else if ((m_HIBYTE == 4))
PressAlt();
else if ((m_HIBYTE == 6))
PressAltGr();
//Press, then release the key.
keybd_event(m_LOWBYTE, sScan, 0, 0); //Key down
keybd_event(m_LOWBYTE, sScan, 0x0002, 0); //Key up
//Release the special key if needed.
if ((m_HIBYTE == 1))
ReleaseShift();
else if ((m_HIBYTE == 2))
ReleaseControl();
else if ((m_HIBYTE == 4))
ReleaseAlt();
else if ((m_HIBYTE == 6))
ReleaseAltGr();
#endregion
//Get the next char from the string.
m_Index++;
}
//Remove the already processed chars from the string.
if (m_Index < m_text.Length)
m_text = m_text.Substring(m_Index);
else
m_text = string.Empty;
}
So, this was the logic that processes a string. Lets see the helper methods that will handle the events:
Press and release special keys are the same, only the first two parameters are different.
Check msdn to get the virtual and scan codes for enter, tab, alt, altgr, etc...
#region [ Press shift ]
private void PressShift()
{
//0xA0 is the virtual key of 'shift'.
//0x2A is the scan code of 'shift'.
keybd_event(0xA0, 0x2A, 0, 0);
}
#endregion
#region [ Release shift ]
private void ReleaseShift()
{
keybd_event(0xA0, 0x2A, 0x0002, 0);
}
#endregion
PressSpecial is similar to the code above, so it can be used for shift as well. I separated some of them into different methods as for me its easier to see what i use in the code (its easier for me to use 'PressShift();' instead of 'PressSpecial("shift")';). :)
private void PressSpecial(string p_Special)
{
switch (p_Special.ToLower()) //<-- use lower version!
{
case "home":
keybd_event(0x24, 0x47, 0, 0);
break;
case "end":
keybd_event(0x23, 0x4F, 0, 0);
break;
//Do the same for any key you need (enter, tab, page up, etc...).
//Remember to get the proper virtual- and scan codes for each keys!
}
}
ReleaseSpecial is the same as PressSpecial, but the 3rd parameter is 0x0002.
private void ReleaseSpecial(string p_Special)
{
switch (p_Special.ToLower())
{
case "home":
keybd_event(0x24, 0x47, 0x0002, 0);
break;
case "end":
keybd_event(0x23, 0x4F, 0x0002, 0);
break;
}
}
And finally, here are the dll import methods. You can put them into a static class, if you wish:
[DllImport("user32.dll", EntryPoint = "keybd_event", CharSet = CharSet.Auto,
ExactSpelling = true)]
public static extern void keybd_event(byte vk, byte scan, int flags, int extrainfo);
[DllImport("user32.dll")]
public static extern IntPtr GetKeyboardLayout(uint idThread);
[DllImport("user32.dll")]
public static extern short VkKeyScanEx(char ch, IntPtr dwhkl);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int MapVirtualKey(int uCode, int uMapType);

Related

How to detect when (forward) slash key is pressed in OEM keys C#

I'm working on wpf c# application and I need to detect when user press "/" but I'm having trouble with finding " / " e.Key, I saw there is Key.OemBackslash and stuffs like that, but I can't find right event for " / " (forward slash) ...
Thanks guys,
Cheers
It should be Key.OemQuestion on a US keyboard. But on a Swedish keyboard it is D7 so it depends. The keys on the keyboard doesn't always produce the same character.
Depending on what you are trying to do you may be better off handling the PreviewTextInput event:
protected override void OnPreviewTextInput(TextCompositionEventArgs e)
{
base.OnPreviewTextInput(e);
if (e.Text == "/")
{
Debug.WriteLine("...");
}
}
You can the following methods (see this site) to get the character from the key.
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
bool toUnicodeIsTrue=false;
char t = GetCharFromKey(e.Key, ref toUnicodeIsTrue);
if ( t == '/')
{
// do stuff
}
base.OnPreviewKeyDown(e);
}
public static char GetCharFromKey(System.Windows.Input.Key key, ref bool toUnicodeIsTrue)
{
toUnicodeIsTrue = true;
char ch = ' ';
// First, you need to get the VirtualKey code. Thankfully, there’s a simple class
// called KeyInterop, which exposes a static method VirtualKeyFromKey
// that gets us this information
int virtualKey = System.Windows.Input.KeyInterop.VirtualKeyFromKey(key);
//Then, we need to get the character. This is much trickier.
//First we have to get the keyboard state and then we have to map that VirtualKey
//we got in the first step to a ScanCode, and finally, convert all of that to Unicode,
//because .Net doesn’t really speak ASCII
byte[] keyboardState = new byte[256];
GetKeyboardState(keyboardState);
uint scanCode = MapVirtualKey((uint)virtualKey, MapType.MAPVK_VK_TO_VSC);
StringBuilder stringBuilder = new StringBuilder(2);
int result = ToUnicode((uint)virtualKey, scanCode, keyboardState, stringBuilder, stringBuilder.Capacity, 0);
switch (result)
{
case -1:
toUnicodeIsTrue = false;
break;
case 0:
toUnicodeIsTrue = false;
break;
case 1:
{
ch = stringBuilder[0];
break;
}
default:
{
ch = stringBuilder[0];
break;
}
}
return ch;
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool GetKeyboardState(byte[] lpKeyState);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern uint MapVirtualKey(uint uCode, MapType uMapType);
public enum MapType : uint
{
MAPVK_VK_TO_VSC = 0x0,
MAPVK_VSC_TO_VK = 0x1,
MAPVK_VK_TO_CHAR = 0x2,
MAPVK_VSC_TO_VK_EX = 0x3,
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int ToUnicode(
uint wVirtKey,
uint wScanCode,
byte[] lpKeyState,
[System.Runtime.InteropServices.Out, System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr, SizeParamIndex = 4)]
StringBuilder pwszBuff,
int cchBuff,
uint wFlags);
}
My old Answer:
protected override void OnPreviewKeyDown(KeyEventArgs e)
{ //***
if (e.Key == Key.Oem2)
{
// do stuff
}
base.OnPreviewKeyDown(e);
}
Note that the name starts with "oem" (Original Equipment Manufacturer), which means the keyboard manufacturer is responsible for its functionality and it varies in local keyboards. So, you can set a break point in
{//***
line of my code and check e.Key property.

Convert keycode to char/string

I'm trying to convert System.Windows.Forms.Keys to string/char using :
KeysConverter converter = new KeysConverter();
string text = converter.ConvertToString(keyCode);
Console.WriteLine(text);
But it returned "OemPeriod" for "." and "Oemcomma" for ",". Is there any way to get the exact character?
What you are trying to achieve is no trivial task by any means. There is the keyboard mapping (keyboard layout) that windows (for example) uses to translate keyboard keys to actual characters. Here is how I was able to achieve this:
public string KeyCodeToUnicode(Keys key)
{
byte[] keyboardState = new byte[255];
bool keyboardStateStatus = GetKeyboardState(keyboardState);
if (!keyboardStateStatus)
{
return "";
}
uint virtualKeyCode = (uint)key;
uint scanCode = MapVirtualKey(virtualKeyCode, 0);
IntPtr inputLocaleIdentifier = GetKeyboardLayout(0);
StringBuilder result = new StringBuilder();
ToUnicodeEx(virtualKeyCode, scanCode, keyboardState, result, (int)5, (uint)0, inputLocaleIdentifier);
return result.ToString();
}
[DllImport("user32.dll")]
static extern bool GetKeyboardState(byte[] lpKeyState);
[DllImport("user32.dll")]
static extern uint MapVirtualKey(uint uCode, uint uMapType);
[DllImport("user32.dll")]
static extern IntPtr GetKeyboardLayout(uint idThread);
[DllImport("user32.dll")]
static extern int ToUnicodeEx(uint wVirtKey, uint wScanCode, byte[] lpKeyState, [Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pwszBuff, int cchBuff, uint wFlags, IntPtr dwhkl);
This is probably what you really want (bit late, but hope this will help someone else), converting the keycode directly to the character the key prints.
First add this directive into your class:
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int ToUnicode(
uint virtualKeyCode,
uint scanCode,
byte[] keyboardState,
StringBuilder receivingBuffer,
int bufferSize,
uint flags
);
Then use this if you just want to ignore the shift modifier
StringBuilder charPressed = new StringBuilder(256);
ToUnicode((uint)keyCode, 0, new byte[256], charPressed, charPressed.Capacity, 0);
Now just call charPressed.ToString() to get your key.
If you want the shift modifier, you can use something like this to make it easier
static string GetCharsFromKeys(Keys keys, bool shift)
{
var buf = new StringBuilder(256);
var keyboardState = new byte[256];
if (shift)
{
keyboardState[(int)Keys.ShiftKey] = 0xff;
}
ToUnicode((uint)keys, 0, keyboardState, buf, 256, 0);
return buf.ToString();
}
since there is "console-application" in your question tags, try this
ConsoleKeyInfo input = Console.ReadKey(true);
StringBuilder output = new StringBuilder(
String.Format("You pressed {0}", input.KeyChar));
Console.WriteLine(output.ToString());
Those are the correct and expected names for the comma and period keys on your keyboard. You can see that clearly from the documentation. The KeysConverter class is behaving as expected and as designed.
If you want to come up with a different name for those keys you can detect them and substitute the name that you desire to use. For instance:
string name;
switch (e.KeyCode)
{
case Keys.Oemcomma:
name = "Comma";
break;
case Keys.OemPeriod:
name = "Period";
break;
default:
name = (new KeysConverter()).ConvertToString(e.KeyCode);
break;
}

C# to C++ process with WM_COPYDATA passing struct with strings

From a c# program I want to use WM_COPYDATA with SendMessage to communicate with a legacy c++/cli MFC application.
I want to pass a managed struct containing string objects.
I can find the handle to the c++ application for use with SendMessage fine.
The bit I don't know about is how the struct and it's strings can be marshalled and read at the other end. Especially as it contains non-blittables.
Do people think this is feasible?
I'll continue to work on it, but would apprecite someone who's done this sort of thing telling me if it just isn't going to work.
Here is some demo code if it was a c++/cli program and it's not difficult to get it working.
However, I'd like this to be in a .Net class library so it can easily be re-used.
//Quick demonstation code only, not correctly styled
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
struct MessageInfo
{
int nVersion;
char szTest[ 10 ];
};
MessageInfo sMessageInfo;
sMessageInfo.nVersion = 100;
strcpy( sMessageInfo.szTest, "TEST");
COPYDATASTRUCT CDS;
CDS.dwData = 1; //just for test
CDS.cbData = sizeof( sMessageInfo );
CDS.lpData = &sMessageInfo;
//find running processes and send them a message
//can't just search for "MYAPP.exe" as will be called "MYAPP.exe *32" on a 64bit machine
array<System::Diagnostics::Process^>^allProcesses = System::Diagnostics::Process::GetProcesses();
for each (System::Diagnostics::Process^ targetProcess in allProcesses)
{
if (targetProcess->ProcessName->StartsWith("MYAPP", System::StringComparison::OrdinalIgnoreCase))
{
HWND handle = static_cast<HWND>(targetProcess->MainWindowHandle.ToPointer());
BOOL bReturnValue = SendMessage( handle, WM_COPYDATA, (WPARAM)0, (LPARAM)&CDS ) == TRUE;
}
}
return 0;
}
I have it working.
A simple approach is to serialize the struct to a single string and transfer a string.
The swhistlesoft blog was helpful http://www.swhistlesoft.com/blog/2011/11/19/1636-wm_copydata-with-net-and-c
This may be enough to provide the simple messaging.
The struct can be re-constructed at the other end if necessary.
If a struct with any number of strings is to be marshalled as-is then it must be a fixed size, that's the main thing I wasn't getting.
The
MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 9)
basically sets the size to match the c++ size which in our case is a TCHAR szTest[ 9 ];
In order to transfer a .Net struct via WM_COPYDATA from c# to c++(/cli) I had to do as follows:
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
static extern bool SetForegroundWindow(IntPtr hWnd);
public static uint WM_COPYDATA = 74;
//from swhistlesoft
public static IntPtr IntPtrAlloc<T>(T param)
{
IntPtr retval = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(param));
System.Runtime.InteropServices.Marshal.StructureToPtr(param, retval, false);
return (retval);
}
//from swhistlesoft
public static void IntPtrFree(IntPtr preAllocated)
{
if (IntPtr.Zero == preAllocated) throw (new Exception("Go Home"));
System.Runtime.InteropServices.Marshal.FreeHGlobal(preAllocated);
preAllocated = IntPtr.Zero;
}
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
struct COPYDATASTRUCT
{
public uint dwData;
public int cbData;
public IntPtr lpData;
}
/// <summary>
/// Dot net version of AppInfo structure. Any changes to the structure needs reflecting here.
/// struct must be a fixed size for marshalling to work, hence the SizeConst entries
/// </summary>
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential, Pack = 1)]
struct AppInfoDotNet
{
public int nVersion;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 9)]
public string test;
};
To send a string:
COPYDATASTRUCT cd = new COPYDATASTRUCT();
cd.dwData = 2;
cd.cbData = parameters.Length + 1;
cd.lpData = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(parameters);
IntPtr cdBuffer = IntPtrAlloc(cd);
messageReceived = ((int)SendMessage(targetProcess.MainWindowHandle, WM_COPYDATA, IntPtr.Zero, cdBuffer)) != 0;
To receive string in C++:
else if(pCDS->dwData == 2)
{
//copydata message
CString csMessage = (LPCTSTR)pCDS->lpData;
OutputDebugString("Copydata message received: " + csMessage);
}
To send struct:
AppInfoDotNet appInfo = new AppInfoDotNet();
appInfo.test = "a test";
COPYDATASTRUCT cds3;
cds3.dwData = 1;
cds3.cbData = System.Runtime.InteropServices.Marshal.SizeOf(appInfo);
IntPtr structPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(System.Runtime.InteropServices.Marshal.SizeOf(appInfo));
System.Runtime.InteropServices.Marshal.StructureToPtr(appInfo, structPtr, false);
cds3.lpData = structPtr;
IntPtr iPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(System.Runtime.InteropServices.Marshal.SizeOf(cds3));
System.Runtime.InteropServices.Marshal.StructureToPtr(cds3, iPtr, false);
messageReceived = ((int)SendMessage(targetProcess.MainWindowHandle, WM_COPYDATA, IntPtr.Zero, iPtr)) != 0;
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(iPtr);
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(structPtr);
To receive struct in C++:
LRESULT CMainFrame::OnCopyData( WPARAM wParam, LPARAM lParam )
{
LRESULT lResult = FALSE;
COPYDATASTRUCT *pCDS = (COPYDATASTRUCT*)lParam;
//Matching message type for struct
if(pCDS->dwData == 1)
{
AppInfo *pAppInfo = (AppInfo*)pCDS->lpData
lResult = true;
}
Please note this is demo code and needs work in terms of styling, exception handling etc, etc...
From the documentation:
The data being passed must not contain pointers or other references to objects not accessible to the application receiving the data.
So you need to pack your string into COPYDATASTRUCT.lpData. If you have a max length for each string then you can embed it in a fixed length structure
typedef struct tagMYDATA
{
char s1[80];
char s2[120];
} MYDATA;
If you have only one variable length string you can put the string at the end and use a header followed by string data
typedef struct tagMYDATA
{
int value1;
float value2;
int stringLen;
} MYDATAHEADER;
MyCDS.cbData = sizeof(MYDATAHEADER)+(int)stringData.size();
MyCDS.lpData = new BYTE[MyCDS.cbData];
memcpy(MyCDS.lpData,&dataHeader,sizeof*(MYDATAHEADER);
StringCbCopyA (
((BYTE*)MyCDS.lpData)+sizeof*(MYDATAHEADER)
,stringData.size()
,stringData.c_str());
If you have multiple variable length strings you can still use a header and allocate more spaces for every strings plus a double null terminator, or serialize everything into one XML string.

SendInput sequence to create unicode character fails

I'm working with an on-screen keyboard that needs to send key strokes to a third party application. They are running on Windows XP. A small set of characters that are not available on the US English keyboard need to be supported (such as "å" or ñ). After reviewing SendInput it seemed like the safest thing would be to send the hex unicode value of the character as a key stroke sequence. I wrote code that sends an "Alt" and "Add" key down event, followed by key down and up events for the four character unicode sequence with the Alt key ORed, then finally "Add" and "Alt" key up events. In my C# test app. I am using KeyPreview and sure enough, all of the events are coming through however all I get is a beep, no character. I have captured the same information from entering the key strokes manually, the KeyPreview information is identical, and the character appears.
Is it possible to use SendInput in this way? I haven't used a hook to examine the data but I've seen posts that indicate SendInput events have some sort of "injected" flag attached, maybe this causes the sequence to fail?
This demo code successfully sends key events, but a sequence of key events intended to generate a unicode character fails.
private const uint KEYEVENTF_KEYDOWN = 0x0000;
private const uint KEYEVENTF_EXTENDEDKEY = 0x0001;
private const uint KEYEVENTF_KEYUP = 0x0002;
private const int INPUT_KEYBOARD = 1;
[DllImport ("user32.dll", SetLastError = false)]
static extern IntPtr GetMessageExtraInfo ();
[DllImport ("user32.dll", SetLastError = true)]
static extern uint SendInput (uint nInputs, [MarshalAs (UnmanagedType.LPArray, SizeConst = 1)] INPUT[] pInputs, int cbSize);
[StructLayout (LayoutKind.Sequential, Size = 24)]
private struct KEYBDINPUT
{
public ushort wVk;
public ushort wScan;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout (LayoutKind.Explicit)]
private struct INPUT
{
[FieldOffset (0)]
public int type;
[FieldOffset (4)]
public KEYBDINPUT ki;
}
private void PressKey (Keys k)
{
PressKeyDown (k);
PressKeyUp (k);
}
private void PressKeyDown (Keys k)
{
INPUT input = new INPUT ();
input.type = INPUT_KEYBOARD;
input.ki.wVk = (byte)k;
input.ki.wScan = 0;
input.ki.time = 0;
uint flags = KEYEVENTF_KEYDOWN;
if ((33 <= (byte)k && (byte)k <= 46) || (91 <= (byte)k) && (byte)k <= 93)
flags |= KEYEVENTF_EXTENDEDKEY;
input.ki.dwFlags = flags;
input.ki.dwExtraInfo = GetMessageExtraInfo ();
Output ("Sending key down {0}. Flags:{1}", k, flags);
INPUT[] inputs = new INPUT[] { input };
uint result = SendInput ((uint)inputs.Length, inputs, Marshal.SizeOf (typeof (INPUT)));
if ((uint)inputs.Length != result)
MessageBox.Show ("PressKeyDown result = " + Marshal.GetLastWin32Error ());
}
private void PressKeyUp (Keys k)
{
INPUT input = new INPUT ();
input.type = INPUT_KEYBOARD;
input.ki.wVk = (byte)k;
input.ki.wScan = 0;
input.ki.time = 0;
uint flags = KEYEVENTF_KEYUP;
if ((33 <= (byte)k && (byte)k <= 46) || (91 <= (byte)k) && (byte)k <= 93)
flags |= KEYEVENTF_EXTENDEDKEY;
input.ki.dwFlags = flags;
input.ki.dwExtraInfo = GetMessageExtraInfo ();
Output ("Sending key up {0}", k);
INPUT[] inputs = new INPUT[] { input };
uint result = SendInput ((uint)inputs.Length, inputs, Marshal.SizeOf (typeof (INPUT)));
if ((uint)inputs.Length != result)
MessageBox.Show ("PressKeyUp result = " + Marshal.GetLastWin32Error ());
}
private void TestSend ()
{
System.Threading.Thread.CurrentThread.Join (1000);
Keys k = Keys.Menu;
PressKeyDown (k);
System.Threading.Thread.Sleep (100);
k = Keys.Add;
k |= Keys.Alt;
PressKeyDown (k);
System.Threading.Thread.Sleep (100);
k = Keys.NumPad0;
k |= Keys.Alt;
PressKey (k);
System.Threading.Thread.Sleep (100);
k = Keys.NumPad0;
k |= Keys.Alt;
PressKey (k);
System.Threading.Thread.Sleep (100);
k = Keys.E;
k |= Keys.Alt;
PressKey (k);
System.Threading.Thread.Sleep (100);
k = Keys.NumPad5;
k |= Keys.Alt;
PressKey (k);
System.Threading.Thread.Sleep (100);
PressKeyUp (Keys.Add);
PressKeyUp (Keys.Menu);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace Simulate
{
public class Simulate
{
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
static extern UInt32 SendInput(UInt32 numberOfInputs, INPUT[] input, Int32 sizeOfInputStructure);
[StructLayout(LayoutKind.Sequential, Size = 24)]
struct KEYBDINPUT
{
public UInt16 Vk;
public UInt16 Scan;
public UInt32 Flags;
public UInt32 Time;
public UInt32 ExtraInfo;
}
[StructLayout(LayoutKind.Explicit)]
private struct INPUT
{
[FieldOffset(0)]
public int Type;
[FieldOffset(4)]
public KEYBDINPUT ki;
}
public static void TextInput(string text)
{
char[] chars = text.ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
UInt16 unicode = chars[i];
INPUT down = new INPUT();
down.Type = 1; //INPUT_KEYBOARD
down.ki.Vk = 0;
down.ki.Scan = unicode;
down.ki.Time = 0;
down.ki.Flags = 0x0004; //KEYEVENTF_UNICODE
down.ki.ExtraInfo = 0;
INPUT up = new INPUT();
up.Type = 1; //INPUT_KEYBOARD
up.ki.Vk = 0;
up.ki.Scan = unicode;
up.ki.Time = 0;
up.ki.Flags = 0x0004; //KEYEVENTF_UNICODE
up.ki.ExtraInfo = 0;
INPUT[] input = new INPUT[2];
input[0] = down;
input[1] = up;
SendInput(1, input, Marshal.SizeOf(typeof(INPUT)));
}
}
}
}
// Call the API :
Simulate.TextInput("AbCçDeFgĞhİiJkLmNoÖpQrSşTuÜvXyZ - äÄß_0123456789");
You can generate them by holding down the Alt key and typing in the 4 digit Unicode codepoint on the numeric keypad. å = Alt + 0229, ñ = Alt + 0241. Find other codes with the Charmap.exe applet.
Apparently the processing of a sequence of key presses to represent a unicode character is done at a level that is not accessible through SendInput. I changed my code to set the unicode flag on dwFlags and set the unicode value on the wScan data parameter. I've managed to convince myself after testing with several European and Asian languages that this creates the same results as the multiple keystroke method.

How can I check and toggle Num-Lock programmatically in C#?

I would like to programmatically check the value of, and be able to toggle num-lock. What's the simplest way to do that in C#?
The reason is that I want to verify num-lock is "ON" at program start.
Thanks
Check How to programmatically turn on the Numlock Key
using System;
using System.Runtime.InteropServices;
class SetNumlockKeyOn
{
[StructLayout(LayoutKind.Sequential)]
public struct INPUT
{
internal int type;
internal short wVk;
internal short wScan;
internal int dwFlags;
internal int time;
internal IntPtr dwExtraInfo;
int dummy1;
int dummy2;
internal int type1;
internal short wVk1;
internal short wScan1;
internal int dwFlags1;
internal int time1;
internal IntPtr dwExtraInfo1;
int dummy3;
int dummy4;
}
[DllImport(“user32.dll”)]
static extern int SendInput(uint nInputs, IntPtr pInputs, int cbSize);
public static void SetNumlockOn()
{
const int mouseInpSize = 28;//Hardcoded size of the MOUSEINPUT tag !!!
INPUT input = new INPUT();
input.type = 0x01; //INPUT_KEYBOARD
input.wVk = 0x90; //VK_NUMLOCK
input.wScan = 0;
input.dwFlags = 0; //key-down
input.time = 0;
input.dwExtraInfo = IntPtr.Zero;
input.type1 = 0x01;
input.wVk1 = 0x90;
input.wScan1 = 0;
input.dwFlags1 = 2; //key-up
input.time1 = 0;
input.dwExtraInfo1 = IntPtr.Zero;
IntPtr pI = Marshal.AllocHGlobal(mouseInpSize * 2);
Marshal.StructureToPtr(input, pI, false);
int result = SendInput(2, pI, mouseInpSize); //Hardcoded size of the MOUSEINPUT tag !!!
//if (result == 0 || Marshal.GetLastWin32Error() != 0)
// Console.WriteLine(Marshal.GetLastWin32Error());
Marshal.FreeHGlobal(pI);
}
You can do this via P/Invoke with GetKeyboardState and keybd_event.
The MSDN page for keybd_event shows exactly how to toggle num-lock, as well as get it's state (in C++).
There are P/Invoke signitures available on pinvoke.net for keybd_event and GetKeyboardState.
In addition to the answer given by Arsen:
There are problems with heap corruption in 64-bit builds. Programs using this code may crash at any point. To see this, enable the debug option "Enable Windows debug heap allocator". The debugger stops on calling FreeHGlobal.
It helps to calculate the size of the INPUT structure as follows.
int mouseInpSize = Marshal.SizeOf(input);
IntPtr pI = Marshal.AllocHGlobal(mouseInpSize);
Marshal.StructureToPtr(input, pI, false);
int result = SendInput(2, pI, mouseInpSize);
Marshal.FreeHGlobal(pI);

Categories

Resources