automate pressing the keyboard key using C# code

  • Thread starter Thread starter raja sekhar
  • Start date Start date
R

raja sekhar

Hi all,

I m doing UI automation, in some ui the default control is at other
key and i want to press the some other button , So I need to press
left arrow key twice & then enter key,
I m new to c# .net , & i don know how to do this.....?
Please if nyone could help...!!
 
Hi all,

I m doing UI automation,  in some ui the default control is at other
key and i want to press the some other button , So I need to press
left arrow key twice & then enter key,
I m new to c# .net , & i don know how to do this.....?
Please if nyone could help...!!

I do not understand what you mean with "is at other key" , could you
explaian better?
 
//example below actually sends key press, key release or keyup and release
//net namespace required System.Runtime.InteropServices;

DaveL

public static class Keybord
{

private const uint INPUT_KEYBOARD = 1;
private const int KEY_EXTENDED = 0x0001;
private const uint KEY_UP = 0x0002;
// private const uint KEY_SCANCODE = 0x0004; //unicode 0x0008 Key_
private const uint KEYEVENTF_UNICODE = 0x0004; //THIS WORKS AS
DEFAULT FLAG
private const uint KEYEVENTF_SCANCODE = 0x0008; //IF I USE THIS IN
THE FLAGS STRANGE RESULTS
[DllImport("User32.dll")]
private static extern uint SendInput(uint numberOfInputs,
[MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] KEYBOARD_INPUT[] input,
int structSize);






public static void SendKeyP(char cKey)
{
ushort iKey = (byte)cKey;
SendKey(iKey,true);
}
/// <summary>
/// Presses a Key
/// </summary>
/// <param name="scanCode"></param>
public static void SendKeyP(int scanCode)
{
SendKey(scanCode, true);
}

public static void SendKeyR(char cKey)
{
ushort iKey = (byte)cKey;
SendKey(iKey,false);
}
/// <summary>
/// Releases Key
/// </summary>
/// <param name="scanCode"></param>
///
public static void SendKeyR(int scanCode)
{
SendKey(scanCode, false);
}
/// <summary>
/// single Char Key, Press and Release
/// </summary>
/// <param name="cKey"></param>
/// <param name="Press"></param>
public static void SendKey(char cKey)
{
int iKey = (Byte)cKey;
SendKey(iKey, true);
SendKey(iKey, false);

}
/// <summary>
/// Single Int KeyCode hex or ascii
/// </summary>
/// <param name="scanCode"></param>
/// <param name="press"></param>
public static void SendKey(int scanCode, bool press)
{
KEYBOARD_INPUT[] input = new KEYBOARD_INPUT[1];
input[0] = new KEYBOARD_INPUT();
input[0].type = INPUT_KEYBOARD;
input[0].flags = KEYEVENTF_UNICODE;

if ((scanCode & 0xFF00) == 0xE000)
{ // extended key?
input[0].flags |= KEY_EXTENDED;
}

if (press)
{ // press?
input[0].scanCode = (ushort)(scanCode & 0xFF);
}
else
{ // release?
input[0].scanCode = (ushort)scanCode;
input[0].flags |= KEY_UP;
}

uint result = SendInput(1, input, Marshal.SizeOf(input[0]));

if (result != 1)
{
throw new Exception("Could not send key: " + scanCode);
}
}


/// <summary>
/// String of 1 to many letters, words, etc example
SendKeys("HelloWorld");
/// this does a Press and Release
/// </summary>
/// <param name="sKeys"></param>
public static void SendKeys(string sKeys)
{
Byte[] aKeys = UTF8Encoding.ASCII.GetBytes(sKeys);
int iLen = aKeys.Length;
ushort scanCode = 0;
uint result;
for (int x = 0; x < iLen; x++)
{
//2 elements for each key, one for press one for Release
scanCode = aKeys[x];
KEYBOARD_INPUT[] input = new KEYBOARD_INPUT[2];
input[0] = new KEYBOARD_INPUT();
input[0].type = INPUT_KEYBOARD;
input[0].flags = KEYEVENTF_UNICODE;

input[1] = new KEYBOARD_INPUT();
input[1].type = INPUT_KEYBOARD;
input[1].flags = KEYEVENTF_UNICODE;
if ((scanCode & 0xFF00) == 0xE000)
{ // extended key?
input[0].flags |= KEY_EXTENDED;
input[1].flags |= KEY_EXTENDED;
}

//input[0] for key press
input[0].scanCode = (ushort)(scanCode & 0xFF);
//input[1] for KeyReleasse
input[1].scanCode = (ushort)scanCode;
input[1].flags |= KEY_UP;
//call sendinput once for both keys
result = SendInput(2, input, Marshal.SizeOf(input[0]));





}
}

}
 
Back
Top