Reputation: 6330
I've researched a few options, mainly;
But I can't find if it's possible to send the right or left variants of system keys to the SendKey.Send()
api. For example it's possible to send the generic shift+A by supplying "+A"
as per the documentation but I need to be able to differentiate between left and right shift, control and alt etc.
Is it even possible with SendKeys.Send()
?
Aside, I'm wondering if there is a solution using something like the Keys enum.
Upvotes: 3
Views: 3017
Reputation: 94
No, it's not possible to differentiate between left and right Shift, Ctrl, and Alt keys with the .NET Framework SendKeys.Send(String) method, but you're on the right track with the Keys
enum. You'll need to use interop services instead.
Since the method you are inquiring about is in the namespace System.Windows.Forms
, I've assumed you're working with WinForms. Below are partial instructions for creating a WinForms app that has six buttons which send Right Shift, Left Shift, Right Alt, Left Alt, Right Ctrl, and Left Ctrl keys to the app's own window. Put a Label on the Form so that it can display whichever of the six keys was pressed. This is made possible by overriding DefWndProc.
You should be able to fill in the missing pieces. Changing the app to send these keystrokes to another application shouldn't be too tough either.
Make sure you have these using statements at the top of the Form1.cs file:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
Inside the Form1.cs class at the top declare:
const int WM_KEYDOWN = 0x100;
IntPtr _hWnd;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, uint msg, int wParam, int lParam);
In the Form1 constructor below the call to InitializeComponent, define an event handler which will allow you to retrieve and store the window handle for your app:
HandleCreated += new EventHandler((sender, e) =>
{
_hWnd = this.Handle;
});
Point all of your buttons to the same click event handler, each button being named accordingly:
void button_Click(object sender, EventArgs e)
{
var button = (Button)sender;
var wParam = new IntPtr();
switch (button.Name)
{
case "buttonSendRightShift":
wParam = (IntPtr)Keys.RShiftKey;
break;
case "buttonSendLeftShift":
wParam = (IntPtr)Keys.LShiftKey;
break;
case "buttonSendRightAlt":
wParam = (IntPtr)Keys.RMenu;
break;
case "buttonSendLeftAlt":
wParam = (IntPtr)Keys.LMenu;
break;
case "buttonSendRightCtrl":
wParam = (IntPtr)Keys.RControlKey;
break;
case "buttonSendLeftCtrl":
wParam = (IntPtr)Keys.LControlKey;
break;
}
SendMessage(_hWnd, WM_KEYDOWN, wParam, 1);
}
Override DefWndProc like so:
protected override void DefWndProc(ref Message m)
{
switch (m.Msg)
{
case WM_KEYDOWN:
switch ((Keys)m.WParam)
{
case Keys.RShiftKey:
LabelKeyPressed.Text = "Right Shift Key Received";
break;
case Keys.LShiftKey:
LabelKeyPressed.Text = "Left Shift Key Received";
break;
case Keys.RMenu:
LabelKeyPressed.Text = "Right Alt Key Received";
break;
case Keys.LMenu:
LabelKeyPressed.Text = "Left Alt Key Received";
break;
case Keys.RControlKey:
LabelKeyPressed.Text = "Right Ctrl Key Received";
break;
case Keys.LControlKey:
LabelKeyPressed.Text = "Left Ctrl Key Received";
break;
}
break;
}
base.DefWndProc(ref m);
}
Upvotes: 2
Reputation: 126
I don't know about SendKey.Send()
, but I know an alternative solution for you.
Using AutoIt. It's an script language for automating windows GUI. The important is that it provide .dll
files to allow .NET use. You just need to download, install it and add reference to .dll
files.
You code will be like this: AutoItX.Send("{RSHIFT}")
Upvotes: 1