user32.dll SendInput send capital accented characters - c#

I'm trying to replace the 'è' character with the 'È' character globally on my computer.
Referring to many answers here on SO Like this I succesfully created a program that replaces for example an 'A' with a 'E' or any other "normal" character. But if I try to pass the È char it doesn't work as expected.
I'm not trying to replace only internally to my app, but globally on every computer click.
This is what I'm doing:
Form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Hide();
KeyInterceptor hook = new KeyInterceptor((int)KeyInterceptor.Modifiers.None, 186, this);
hook.Register();
}
protected override void WndProc(ref Message m)
{
Console.WriteLine(m.Msg);
if (m.Msg == 0x0312)
HandleHotkey(); // è, which was registered before, was pressed
base.WndProc(ref m);
}
private void HandleHotkey()
{
// instead of è send È
KeyboardManager.PressKey(459007);
}
}
KeyInterceptor.cs
class KeyInterceptor
{
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
public enum Modifiers
{
None = 0x0000,
Alt = 0x0001,
Control = 0x0002,
Shift = 0x0004,
Win = 0x0008
}
int modifier;
int key;
IntPtr hWnd;
int id;
public KeyInterceptor(int modifiers, Keys key, Form f)
{
this.modifier = modifiers;
this.key = (int)key;
this.hWnd = f.Handle;
id = this.GetHashCode();
}
public KeyInterceptor(int modifiers, char key, Form f)
{
this.modifier = modifiers;
this.key = (int)key;
this.hWnd = f.Handle;
id = this.GetHashCode();
}
public KeyInterceptor(int modifiers, int key, Form f)
{
this.modifier = modifiers;
this.key = key;
this.hWnd = f.Handle;
id = this.GetHashCode();
}
public override int GetHashCode()
{
return modifier ^ key ^ hWnd.ToInt32();
}
public bool Register()
{
return RegisterHotKey(hWnd, id, modifier, key);
}
public bool Unregister()
{
return UnregisterHotKey(hWnd, id);
}
}
KeyboardManager.cs
public class KeyboardManager
{
public const int INPUT_KEYBOARD = 1;
public const int KEYEVENTF_KEYUP = 0x0002;
public struct KEYDBINPUT
{
public Int16 wVk;
public ushort wScan;
public Int32 dwFlags;
public Int32 time;
public Int32 dwExtraInfo;
public Int32 __filler1;
public Int32 __filler2;
}
public struct INPUT
{
public Int32 type;
public KEYDBINPUT ki;
}
[DllImport("user32.dll")]
public static extern int SendInput(int cInputs, ref INPUT pInputs, int cbSize);
public static void HoldKey(Keys vk)
{
INPUT input = new INPUT();
input.type = INPUT_KEYBOARD;
input.ki.dwFlags = 0;
input.ki.wVk = (Int16)vk;
SendInput(1, ref input, Marshal.SizeOf(input));
}
public static void ReleaseKey(Keys vk)
{
INPUT input = new INPUT();
input.type = INPUT_KEYBOARD;
input.ki.dwFlags = KEYEVENTF_KEYUP;
input.ki.wVk = (Int16)vk;
SendInput(1, ref input, Marshal.SizeOf(input));
}
public static void HoldKey(int vk)
{
INPUT input = new INPUT();
input.type = INPUT_KEYBOARD;
input.ki.dwFlags = 0;
input.ki.wVk = (Int16)vk;
SendInput(1, ref input, Marshal.SizeOf(input));
}
public static void ReleaseKey(int vk)
{
INPUT input = new INPUT();
input.type = INPUT_KEYBOARD;
input.ki.dwFlags = KEYEVENTF_KEYUP;
input.ki.wVk = (Int16)vk;
SendInput(1, ref input, Marshal.SizeOf(input));
}
public static void PressKey(Keys vk)
{
HoldKey(vk);
ReleaseKey(vk);
}
public static void PressKey(int vk)
{
HoldKey(vk);
ReleaseKey(vk);
}
}
What am I missing? I don't really get how to send that key out.

Related

C# how to use NM_CUSTOMDRAW to native trackBar control

I use the code below to modify native trackbar.
My questions are:
How to detect if mouse is over thumb and change its color?
How to change the shape of thumb? (How to keep the original shape?)
public class TrackBarCustom : TrackBar
{
private Color tbarColor;
private Color tbarBorder;
private Color thumbColor;
private Color thumbBorder;
public TrackBarCustom()
{
}
// custom draw item specs
private const int TBCD_TICS = 0x1;
private const int TBCD_THUMB = 0x2;
private const int TBCD_CHANNEL = 0x3;
[StructLayout(LayoutKind.Sequential)]
public struct NMHDR
{
public IntPtr hwndFrom;
public IntPtr idFrom;
public int code;
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[StructLayout(LayoutKind.Sequential)]
public struct NMCUSTOMDRAW
{
public NMHDR hdr;
public int dwDrawStage;
public IntPtr hdc;
public RECT rc;
public IntPtr dwItemSpec;
public uint uItemState;
public IntPtr lItemlParam;
}
[Flags]
public enum CDRF
{
CDRF_DODEFAULT = 0x0,
CDRF_NEWFONT = 0x2,
CDRF_SKIPDEFAULT = 0x4,
CDRF_DOERASE = 0x8,
CDRF_SKIPPOSTPAINT = 0x100,
CDRF_NOTIFYPOSTPAINT = 0x10,
CDRF_NOTIFYITEMDRAW = 0x20,
CDRF_NOTIFYSUBITEMDRAW = 0x20,
CDRF_NOTIFYPOSTERASE = 0x40
}
[Flags]
public enum CDDS
{
CDDS_PREPAINT = 0x1,
CDDS_POSTPAINT = 0x2,
CDDS_PREERASE = 0x3,
CDDS_POSTERASE = 0x4,
CDDS_ITEM = 0x10000,
CDDS_ITEMPREPAINT = (CDDS.CDDS_ITEM | CDDS.CDDS_PREPAINT),
CDDS_ITEMPOSTPAINT = (CDDS.CDDS_ITEM | CDDS.CDDS_POSTPAINT),
CDDS_ITEMPREERASE = (CDDS.CDDS_ITEM | CDDS.CDDS_PREERASE),
CDDS_ITEMPOSTERASE = (CDDS.CDDS_ITEM | CDDS.CDDS_POSTERASE),
CDDS_SUBITEM = 0x20000
}
[DllImport("User32.dll", SetLastError = true)]
public static extern int FillRect(IntPtr hDC, ref RECT lpRect, IntPtr hBR);
[DllImport("User32.dll", SetLastError = true)]
public static extern int FrameRect(IntPtr hDC, ref RECT lpRect, IntPtr hPN);
[DllImport("gdi32")]
public static extern IntPtr CreatePen(int penStyle, int width, int color);
[DllImport("Gdi32.dll", SetLastError = true)]
public static extern IntPtr CreateSolidBrush(int crColor);
[DllImport("Gdi32.dll", SetLastError = true)]
public static extern bool DeleteObject(IntPtr hObject);
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_REFLECT + WM_NOFITY)
{
var pnmhdr = (NMHDR)m.GetLParam(typeof(NMHDR));
if (pnmhdr.code == NM_CUSTOMDRAW)
{
var pnmlv = (NMCUSTOMDRAW)m.GetLParam(typeof(NMCUSTOMDRAW));
switch (pnmlv.dwDrawStage)
{
case (int)CDDS.CDDS_PREPAINT:
{
m.Result = new IntPtr((int)CDRF.CDRF_NOTIFYITEMDRAW);
break;
}
case (int)CDDS.CDDS_ITEMPREPAINT:
{
if (((int)pnmlv.dwItemSpec == TBCD_THUMB))
{
IntPtr hBrush = CreateSolidBrush(ColorTranslator.ToWin32(Enabled ? ThumbColor : Color.Silver));
IntPtr hPen = CreatePen(0, 1, ColorTranslator.ToWin32(Enabled ? ThumbBorder : Color.DarkGray));
FillRect(pnmlv.hdc, ref pnmlv.rc, hBrush);
FrameRect(pnmlv.hdc, ref pnmlv.rc, hPen);
DeleteObject(hBrush);
DeleteObject(hPen);
m.Result = new IntPtr((int)CDRF.CDRF_SKIPDEFAULT);
}
else if (((int)pnmlv.dwItemSpec == TBCD_CHANNEL))
{
IntPtr hBarBrush = CreateSolidBrush(ColorTranslator.ToWin32(Enabled ? TbarColor : Color.Silver));
IntPtr hBarPen = CreatePen(0, 1, ColorTranslator.ToWin32(Enabled ? TbarBorder : Color.DarkGray));
FillRect(pnmlv.hdc, ref pnmlv.rc, hBarBrush);
FrameRect(pnmlv.hdc, ref pnmlv.rc, hBarPen);
DeleteObject(hBarBrush);
DeleteObject(hBarPen);
m.Result = new IntPtr((int)CDRF.CDRF_SKIPDEFAULT);
}
else
m.Result = new IntPtr((int)CDRF.CDRF_NOTIFYPOSTPAINT);
break;
}
case (int)CDDS.CDDS_ITEMPOSTPAINT:
{
m.Result = new IntPtr((int)CDRF.CDRF_DODEFAULT);
break;
}
}
}
return;
}
else
base.WndProc(ref m);
}
private const int NM_FIRST = 0;
private const int NM_CLICK = NM_FIRST - 2;
private const int NM_CUSTOMDRAW = NM_FIRST - 12;
private const int WM_REFLECT = 0x2000;
private const int WM_NOFITY = 0x4E;
public Color ThumbColor
{
get
{
return thumbColor;
}
set
{
thumbColor = value;
Update();
Refresh();
Invalidate();
}
}
public Color ThumbBorder
{
get
{
return thumbBorder;
}
set
{
thumbBorder = value;
Update();
Refresh();
Invalidate();
}
}
public Color TbarColor
{
get
{
return tbarColor;
}
set
{
tbarColor = value;
Update();
Refresh();
Invalidate();
}
}
public Color TbarBorder
{
get
{
return tbarBorder;
}
set
{
tbarBorder = value;
Update();
Refresh();
Invalidate();
}
}
}

System Driver Listing In c#

i am working on listing installed drivers in system as in Device manager.
I got that list from Win32_PnPSignedDriver but it does not provide icons. Is there any way to find that or i have to add custom icons for the list. I want to generate output Like same as in Device manager.I found some reference in C/C++ but not in c#.
Here are two utility classes DeviceClass and Device that demonstrate how to do it. They don't use WMI, but the setupapi with p/invoke (and the System.Drawing's Icon class). I think this API is the only supported way to get the icon. You could then use WMI (using ids, and class ids to get more information this time using WMI).
Here is a comparison using these classes, in a Windows Form, and the Device Manager, as you see it's pretty close, but the class icon is not exactly the same, I don't know why, I've not investigated.
Last note: as expected, some devices have an icon that is different from their class and different from other icons in the same class. We can observe that in the first Audio class, the two devices don't have the same icon.
Here is the code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// This is a standard Form. I have added a TreeView control and an ImageList to the Form.
// The image list is bound to the treeview, with ColorDepth set to Depth32Bit
var classes = DeviceClass.Load(DeviceFiter.AllClasses | DeviceFiter.Present);
foreach (var cls in classes)
{
var classNode = treeView1.Nodes.Add(cls.Description);
imageList1.Images.Add(cls.Icon);
classNode.ImageIndex = imageList1.Images.Count - 1;
classNode.SelectedImageIndex = classNode.ImageIndex;
foreach (var device in cls.Devices)
{
var deviceNode = classNode.Nodes.Add(device.Name);
imageList1.Images.Add(device.Icon);
deviceNode.ImageIndex = imageList1.Images.Count - 1;
deviceNode.SelectedImageIndex = deviceNode.ImageIndex;
}
classNode.Expand();
}
// dispose (icons)
foreach (var cls in classes)
{
foreach (var device in cls.Devices)
{
device.Dispose();
}
cls.Dispose();
}
}
}
public class DeviceClass : IDisposable, IComparable, IComparable<DeviceClass>
{
private List<Device> _devices = new List<Device>();
private Icon _icon;
internal DeviceClass(Guid classId, string description)
{
ClassId = classId;
Description = description;
}
public Guid ClassId { get; }
public string Description { get; }
public Icon Icon => _icon;
public IReadOnlyList<Device> Devices => _devices;
public static IReadOnlyList<DeviceClass> Load(DeviceFiter fiter)
{
var list = new List<DeviceClass>();
var hdevinfo = SetupDiGetClassDevs(IntPtr.Zero, null, IntPtr.Zero, fiter);
try
{
var data = new SP_DEVINFO_DATA();
data.cbSize = Marshal.SizeOf<SP_DEVINFO_DATA>();
int index = 0;
while (SetupDiEnumDeviceInfo(hdevinfo, index, ref data))
{
index++;
var classId = GetGuidProperty(hdevinfo, ref data, DEVPKEY_Device_ClassGuid);
if (classId == Guid.Empty)
continue;
string classDescription = GetClassDescription(classId);
var cls = list.FirstOrDefault(c => c.ClassId == classId);
if (cls == null)
{
cls = new DeviceClass(classId, classDescription);
list.Add(cls);
SetupDiLoadClassIcon(ref classId, out IntPtr clsIcon, out int mini);
if (clsIcon != IntPtr.Zero)
{
cls._icon = Icon.FromHandle(clsIcon);
}
}
string name = GetStringProperty(hdevinfo, ref data, DEVPKEY_Device_FriendlyName);
if (string.IsNullOrWhiteSpace(name))
{
name = GetStringProperty(hdevinfo, ref data, DEVPKEY_Device_DeviceDesc);
}
Icon icon = null;
SetupDiLoadDeviceIcon(hdevinfo, ref data, 16, 16, 0, out IntPtr devIcon);
if (devIcon != IntPtr.Zero)
{
icon = Icon.FromHandle(devIcon);
}
var dev = new Device(cls, name, icon);
cls._devices.Add(dev);
}
}
finally
{
if (hdevinfo != IntPtr.Zero)
{
SetupDiDestroyDeviceInfoList(hdevinfo);
}
}
foreach (var cls in list)
{
cls._devices.Sort();
}
list.Sort();
return list;
}
int IComparable.CompareTo(object obj) => CompareTo(obj as DeviceClass);
public int CompareTo(DeviceClass other)
{
if (other == null)
throw new ArgumentNullException(nameof(other));
return Description.CompareTo(other.Description);
}
public void Dispose()
{
if (_icon != null)
{
_icon.Dispose();
_icon = null;
}
}
private static string GetClassDescription(Guid classId)
{
SetupDiGetClassDescription(ref classId, IntPtr.Zero, 0, out int size);
if (size == 0)
return null;
var ptr = Marshal.AllocCoTaskMem(size * 2);
try
{
if (!SetupDiGetClassDescription(ref classId, ptr, size, out size))
throw new Win32Exception(Marshal.GetLastWin32Error());
return Marshal.PtrToStringUni(ptr, size - 1);
}
finally
{
Marshal.FreeCoTaskMem(ptr);
}
}
private static string GetStringProperty(IntPtr hdevinfo, ref SP_DEVINFO_DATA data, DEVPROPKEY pk)
{
SetupDiGetDeviceProperty(hdevinfo, ref data, ref pk, out int propertyType, IntPtr.Zero, 0, out int size, 0);
if (size == 0)
return null;
var ptr = Marshal.AllocCoTaskMem(size);
try
{
if (!SetupDiGetDeviceProperty(hdevinfo, ref data, ref pk, out propertyType, ptr, size, out size, 0))
throw new Win32Exception(Marshal.GetLastWin32Error());
return Marshal.PtrToStringUni(ptr, (size / 2) - 1);
}
finally
{
Marshal.FreeCoTaskMem(ptr);
}
}
private static Guid GetGuidProperty(IntPtr hdevinfo, ref SP_DEVINFO_DATA data, DEVPROPKEY pk)
{
SetupDiGetDeviceProperty(hdevinfo, ref data, ref pk, out int propertyType, out Guid guid, 16, out int size, 0);
return guid;
}
[StructLayout(LayoutKind.Sequential)]
private struct SP_DEVINFO_DATA
{
public int cbSize;
public Guid ClassGuid;
public int DevInst;
public IntPtr Reserved;
}
[StructLayout(LayoutKind.Sequential)]
private struct DEVPROPKEY
{
public Guid fmtid;
public int pid;
}
private const int ERROR_NOT_FOUND = 118;
private const int ERROR_INSUFFICIENT_BUFFER = 122;
private static readonly DEVPROPKEY DEVPKEY_Device_DeviceDesc = new DEVPROPKEY { fmtid = new Guid("a45c254e-df1c-4efd-8020-67d146a850e0"), pid = 2 };
private static readonly DEVPROPKEY DEVPKEY_Device_FriendlyName = new DEVPROPKEY { fmtid = new Guid("a45c254e-df1c-4efd-8020-67d146a850e0"), pid = 14 };
private static readonly DEVPROPKEY DEVPKEY_Device_Class = new DEVPROPKEY { fmtid = new Guid("a45c254e-df1c-4efd-8020-67d146a850e0"), pid = 9 };
private static readonly DEVPROPKEY DEVPKEY_Device_ClassGuid = new DEVPROPKEY { fmtid = new Guid("a45c254e-df1c-4efd-8020-67d146a850e0"), pid = 10 };
[DllImport("setupapi", CharSet = CharSet.Unicode)]
private static extern IntPtr SetupDiGetClassDevs(IntPtr ClassGuid, [MarshalAs(UnmanagedType.LPWStr)] string Enumerator, IntPtr hwndParent, DeviceFiter Flags);
[DllImport("setupapi", SetLastError = true)]
private static extern bool SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);
[DllImport("setupapi", SetLastError = true)]
private static extern bool SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, int MemberIndex, ref SP_DEVINFO_DATA DeviceInfoData);
[DllImport("setupapi", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool SetupDiGetClassDescription(ref Guid ClassGuid, IntPtr ClassDescription, int ClassDescriptionSize, out int RequiredSize);
[DllImport("setupapi", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool SetupDiLoadClassIcon(ref Guid ClassGuid, out IntPtr LargeIcon, out int MiniIconIndex);
[DllImport("setupapi", SetLastError = true)]
private static extern bool SetupDiLoadDeviceIcon(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData,
int cxIcon, int cyIcon, int Flags, out IntPtr hIcon);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool SetupDiGetDeviceProperty(IntPtr DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData,
ref DEVPROPKEY PropertyKey,
out int PropertyType,
IntPtr PropertyBuffer,
int PropertyBufferSize,
out int RequiredSize,
int Flags);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool SetupDiGetDeviceProperty(IntPtr DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData,
ref DEVPROPKEY PropertyKey,
out int PropertyType,
out Guid PropertyBuffer,
int PropertyBufferSize,
out int RequiredSize,
int Flags);
}
[Flags]
public enum DeviceFiter // DIGCF_* flags
{
Default = 1,
Present = 2,
AllClasses = 4,
Profile = 8,
DeviceInterface = 16
}
public class Device : IDisposable, IComparable, IComparable<Device>
{
internal Device(DeviceClass cls, string name, Icon icon)
{
Class = cls;
Name = name;
Icon = icon;
}
public string Name { get; }
public DeviceClass Class { get; }
public Icon Icon { get; private set; }
public override string ToString() => Name;
public void Dispose()
{
if (Icon != null)
{
Icon.Dispose();
Icon = null;
}
}
int IComparable.CompareTo(object obj) => CompareTo(obj as Device);
public int CompareTo(Device other)
{
if (other == null)
throw new ArgumentNullException(nameof(other));
return Name.CompareTo(other.Name);
}
}
There is a nice article using SHGetFileInfo function to get associated icons, i'd recommend looking into that.
You cannot do it using pure .Net or C# you will need this dll in the article.
EDIT
Maybe this MDSN Device Information will be of help
I think many of us having some errors with #Simon's codes.
Here are his revised codes:
public class DeviceClass : IDisposable, IComparable, IComparable<DeviceClass>
{
private List<Device> _devices = new List<Device>();
private Icon _icon;
internal DeviceClass(Guid classId, string description)
{
ClassId = classId;
Description = description;
}
public Guid ClassId { get; }
public string Description { get; }
public Icon Icon => _icon;
public IReadOnlyList<Device> Devices => _devices;
public static IReadOnlyList<DeviceClass> Load(DeviceFiter fiter)
{
var list = new List<DeviceClass>();
var hdevinfo = SetupDiGetClassDevs(IntPtr.Zero, null, IntPtr.Zero, fiter);
try
{
var data = new SP_DEVINFO_DATA();
data.cbSize = System.Runtime.InteropServices.Marshal.SizeOf<SP_DEVINFO_DATA>();
int index = 0;
while (SetupDiEnumDeviceInfo(hdevinfo, index, ref data))
{
index++;
var classId = GetGuidProperty(hdevinfo, ref data, DEVPKEY_Device_ClassGuid);
if (classId == Guid.Empty)
continue;
string classDescription = GetClassDescription(classId);
var cls = list.FirstOrDefault(c => c.ClassId == classId);
if (cls == null)
{
cls = new DeviceClass(classId, classDescription);
list.Add(cls);
IntPtr clsIcon;
int mini;
SetupDiLoadClassIcon(ref classId, out clsIcon, out mini);
if (clsIcon != IntPtr.Zero)
{
cls._icon = Icon.FromHandle(clsIcon);
}
}
string name = GetStringProperty(hdevinfo, ref data, DEVPKEY_Device_FriendlyName);
if (string.IsNullOrWhiteSpace(name))
{
name = GetStringProperty(hdevinfo, ref data, DEVPKEY_Device_DeviceDesc);
}
Icon icon = null;
IntPtr devIcon;
SetupDiLoadDeviceIcon(hdevinfo, ref data, 16, 16, 0, out devIcon);
if (devIcon != IntPtr.Zero)
{
icon = Icon.FromHandle(devIcon);
}
var dev = new Device(cls, name, icon);
cls._devices.Add(dev);
}
}
finally
{
if (hdevinfo != IntPtr.Zero)
{
SetupDiDestroyDeviceInfoList(hdevinfo);
}
}
foreach (var cls in list)
{
cls._devices.Sort();
}
list.Sort();
return list;
}
int IComparable.CompareTo(object obj) => CompareTo(obj as DeviceClass);
public int CompareTo(DeviceClass other)
{
if (other == null)
throw new ArgumentNullException(nameof(other));
return Description.CompareTo(other.Description);
}
public void Dispose()
{
if (_icon != null)
{
_icon.Dispose();
_icon = null;
}
}
private static string GetClassDescription(Guid classId)
{
int size = 0;
SetupDiGetClassDescription(ref classId, IntPtr.Zero, 0, out size);
if (size == 0)
return null;
var ptr = Marshal.AllocCoTaskMem(size * 2);
try
{
if (!SetupDiGetClassDescription(ref classId, ptr, size, out size))
throw new Win32Exception(Marshal.GetLastWin32Error());
return Marshal.PtrToStringUni(ptr, size - 1);
}
finally
{
Marshal.FreeCoTaskMem(ptr);
}
}
private static string GetStringProperty(IntPtr hdevinfo, ref SP_DEVINFO_DATA data, DEVPROPKEY pk)
{
int propertyType;
int size;
SetupDiGetDeviceProperty(hdevinfo, ref data, ref pk, out propertyType, IntPtr.Zero, 0, out size, 0);
if (size == 0)
return null;
var ptr = Marshal.AllocCoTaskMem(size);
try
{
if (!SetupDiGetDeviceProperty(hdevinfo, ref data, ref pk, out propertyType, ptr, size, out size, 0))
throw new Win32Exception(Marshal.GetLastWin32Error());
return Marshal.PtrToStringUni(ptr, (size / 2) - 1);
}
finally
{
Marshal.FreeCoTaskMem(ptr);
}
}
private static Guid GetGuidProperty(IntPtr hdevinfo, ref SP_DEVINFO_DATA data, DEVPROPKEY pk)
{
int propertyType, size;
Guid guid;
SetupDiGetDeviceProperty(hdevinfo, ref data, ref pk, out propertyType, out guid, 16, out size, 0);
return guid;
}
[StructLayout(LayoutKind.Sequential)]
private struct SP_DEVINFO_DATA
{
public int cbSize;
public Guid ClassGuid;
public int DevInst;
public IntPtr Reserved;
}
[StructLayout(LayoutKind.Sequential)]
private struct DEVPROPKEY
{
public Guid fmtid;
public int pid;
}
private const int ERROR_NOT_FOUND = 118;
private const int ERROR_INSUFFICIENT_BUFFER = 122;
private static readonly DEVPROPKEY DEVPKEY_Device_DeviceDesc = new DEVPROPKEY { fmtid = new Guid("a45c254e-df1c-4efd-8020-67d146a850e0"), pid = 2 };
private static readonly DEVPROPKEY DEVPKEY_Device_FriendlyName = new DEVPROPKEY { fmtid = new Guid("a45c254e-df1c-4efd-8020-67d146a850e0"), pid = 14 };
private static readonly DEVPROPKEY DEVPKEY_Device_Class = new DEVPROPKEY { fmtid = new Guid("a45c254e-df1c-4efd-8020-67d146a850e0"), pid = 9 };
private static readonly DEVPROPKEY DEVPKEY_Device_ClassGuid = new DEVPROPKEY { fmtid = new Guid("a45c254e-df1c-4efd-8020-67d146a850e0"), pid = 10 };
[DllImport("setupapi", CharSet = CharSet.Unicode)]
private static extern IntPtr SetupDiGetClassDevs(IntPtr ClassGuid, [MarshalAs(UnmanagedType.LPWStr)] string Enumerator, IntPtr hwndParent, DeviceFiter Flags);
[DllImport("setupapi", SetLastError = true)]
private static extern bool SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);
[DllImport("setupapi", SetLastError = true)]
private static extern bool SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, int MemberIndex, ref SP_DEVINFO_DATA DeviceInfoData);
[DllImport("setupapi", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool SetupDiGetClassDescription(ref Guid ClassGuid, IntPtr ClassDescription, int ClassDescriptionSize, out int RequiredSize);
[DllImport("setupapi", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool SetupDiLoadClassIcon(ref Guid ClassGuid, out IntPtr LargeIcon, out int MiniIconIndex);
[DllImport("setupapi", SetLastError = true)]
private static extern bool SetupDiLoadDeviceIcon(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData,
int cxIcon, int cyIcon, int Flags, out IntPtr hIcon);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool SetupDiGetDeviceProperty(IntPtr DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData,
ref DEVPROPKEY PropertyKey,
out int PropertyType,
IntPtr PropertyBuffer,
int PropertyBufferSize,
out int RequiredSize,
int Flags);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool SetupDiGetDeviceProperty(IntPtr DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData,
ref DEVPROPKEY PropertyKey,
out int PropertyType,
out Guid PropertyBuffer,
int PropertyBufferSize,
out int RequiredSize,
int Flags);
}
[Flags]
public enum DeviceFiter // DIGCF_* flags
{
Default = 1,
Present = 2,
AllClasses = 4,
Profile = 8,
DeviceInterface = 16
}
public class Device : IDisposable, IComparable, IComparable<Device>
{
internal Device(DeviceClass cls, string name, Icon icon)
{
Class = cls;
Name = name;
Icon = icon;
}
public string Name { get; }
public DeviceClass Class { get; }
public Icon Icon { get; private set; }
public override string ToString() => Name;
public void Dispose()
{
if (Icon != null)
{
Icon.Dispose();
Icon = null;
}
}
int IComparable.CompareTo(object obj) => CompareTo(obj as Device);
public int CompareTo(Device other)
{
if (other == null)
throw new ArgumentNullException(nameof(other));
return Name.CompareTo(other.Name);
}
}

How to register a hotkey for (LETTER) and for SHIFT+(LETTER) at the same time

I'm building an application in C# to change the keyboard output for some keys
using globl hotkeys.
when I register a hotkey for a key and then another hotkey for that key + modifier only the last one works
for example
if I do
RegisterHotKey(MyForm.Handle, 100, 0, (int)Keys.T); // T
RegisterHotKey(MyForm.Handle, 200, 4, (int)Keys.T); // Shift + T
it capture only Shift + T press.
And when I do
RegisterHotKey(MyForm.Handle, 100, 4, (int)Keys.T); // Shift + T
RegisterHotKey(MyForm.Handle, 200, 0, (int)Keys.T); // T
It captures only "T" press
Is there a way to handle the two cases ?
Update
that's my code
public partial class Form1 : Form
{
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private KeyHandler ghk;
public Dictionary<Keys, string> ckeys = new Dictionary<Keys, string>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
registerApp();
}
private void registerKey(Keys k, string o, int m = 0)
{
ghk = new KeyHandler(k, this, o, m);
ghk.Register();
}
private void HandleHotkey(Keys id)
{
SendKeys.Send(ckeys[id]);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == Constants.WM_HOTKEY_MSG_ID)
{
int id = m.WParam.ToInt32();
HandleHotkey((Keys)id);
}
base.WndProc(ref m);
}
private void registerApp()
{
registerKey(Keys.T, "ت");
registerKey(Keys.T, "ط", KeyModifier.Shift);
}
//Modifiers
static class KeyModifier
{
public static int None = 0;
public static int Alt = 1;
public static int Control = 2;
public static int Shift = 4;
public static int WinKey = 8;
}
}
//Class KeyHandler
public class KeyHandler
{
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private int key;
private IntPtr hWnd;
private int id;
private int modifier;
public KeyHandler(Keys key, Form1 form, string output, int modifier)
{
this.key = (int)key;
this.modifier = modifier;
this.hWnd = form.Handle;
id = this.key;
form.ckeys[key] = output;
}
public override int GetHashCode()
{
return key ^ hWnd.ToInt32();
}
public bool Register()
{
return RegisterHotKey(hWnd, id, modifier, key);
}
public bool Unregiser()
{
return UnregisterHotKey(hWnd, id);
}
}
//Constants
public static class Constants
{
//windows message id for hotkey
public const int WM_HOTKEY_MSG_ID = 0x0312;
}
The bug is that T and Shift+T are registred with the same id.
I solved the problem by registring Shift+T with a different id .

Send Special Keys Over Remote Desktop

I am able to send special keys on local machine but the same thing is not working on remote machine. I referred to many articles but could not find the code to send special keys to remote desktop connection. Please help on this. Below is the code.
static void Main(string[] args)
{
Thread.Sleep(3000);
//char[] keyboardStrokes = { (char)Keys.LWin, (char)Keys.R };
char[] keyboardStrokes = { (char)Keys.LMenu, (char)Keys.F4 };
SendData(keyboardStrokes);
}
struct INPUT
{
public INPUTType type;
public INPUTUnion Event;
}
[StructLayout(LayoutKind.Explicit)]
struct INPUTUnion
{
[FieldOffset(0)]
internal MOUSEINPUT mi;
[FieldOffset(0)]
internal KEYBDINPUT ki;
[FieldOffset(0)]
internal HARDWAREINPUT hi;
}
[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT
{
public int dx;
public int dy;
public int mouseData;
public int dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct KEYBDINPUT
{
public short wVk;
public short wScan;
public KEYEVENTF dwFlags;
public int time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
public int uMsg;
public short wParamL;
public short wParamH;
}
enum INPUTType : uint
{
INPUT_KEYBOARD = 1
}
[Flags]
enum KEYEVENTF : uint
{
EXTENDEDKEY = 0x0001,
KEYUP = 0x0002,
SCANCODE = 0x0008,
UNICODE = 0x0004
}
[DllImport("user32.dll", SetLastError = true)]
static extern UInt32 SendInput(int numberOfInputs, INPUT[] inputs, int sizeOfInputStructure);
[DllImport("user32.dll")]
static extern IntPtr GetMessageExtraInfo();
private static void SendData(char[] c)
{
ProcessKey(c);
}
private static void ProcessKey(char[] key)
{
// create input events as unicode with first down, then up
INPUT[] inputs = new INPUT[key.Length + 1];
for (int i = 0; i < key.Length; i++)
{
inputs[i].type = INPUTType.INPUT_KEYBOARD;
inputs[i].Event.ki.dwFlags = KEYEVENTF.UNICODE;
inputs[i].Event.ki.wScan = (short)key[i];
inputs[i].Event.ki.wVk = (short)key[i];
}
//Thread.Sleep(3000);
inputs[key.Length].type = INPUTType.INPUT_KEYBOARD;
inputs[key.Length].Event.ki.dwFlags = KEYEVENTF.KEYUP;
inputs[key.Length].Event.ki.dwExtraInfo = GetMessageExtraInfo();
// inputs[key.Length].Event.ki.wScan =
// inputs[key.Length].Event.ki.dwFlags |= KEYEVENTF.KEYUP;
uint cSuccess = SendInput(inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
if (cSuccess != inputs.Length)
{
throw new Win32Exception();
}
}
Thanks in advance.
Finally I am able to send most of the special keys to remote machine by using following code. The only issue is that special keys remain pressed after the operation is completed. please let me know how to release the special keys.
class Program
{
static void Main(string[] args)
{
Thread.Sleep(3000);
int[] keyboardStrokes = { (int)Keys.LMenu, (int)Keys.Tab, (int)Keys.Tab };
ProcessKey(keyboardStrokes);
Console.Read();
}
struct INPUT
{
public INPUTType type;
public INPUTUnion Event;
}
[StructLayout(LayoutKind.Explicit)]
struct INPUTUnion
{
[FieldOffset(0)]
internal MOUSEINPUT mi;
[FieldOffset(0)]
internal KEYBDINPUT ki;
[FieldOffset(0)]
internal HARDWAREINPUT hi;
}
[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT
{
public int dx;
public int dy;
public int mouseData;
public int dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct KEYBDINPUT
{
public ushort wVk;
public ushort wScan;
public KEYEVENTF dwFlags;
public int time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
public int uMsg;
public short wParamL;
public short wParamH;
}
enum INPUTType : uint
{
INPUT_KEYBOARD = 1
}
[Flags]
enum KEYEVENTF : uint
{
EXTENDEDKEY = 0x0001,
KEYUP = 0x0002,
SCANCODE = 0x0008,
UNICODE = 0x0004
}
[DllImport("user32.dll", SetLastError = true)]
static extern UInt32 SendInput(int numberOfInputs, INPUT[] inputs, int sizeOfInputStructure);
[System.Runtime.InteropServices.DllImport("user32.dll")]
internal static extern uint MapVirtualKey(uint uCode, uint uMapType);
private static void ProcessKey(int[] key)
{
INPUT[] inputs = new INPUT[key.Length + 1];
for (int i = 0; i < key.Length; i++)
{
uint skey = MapVirtualKey((uint)key[i], (uint)0x0);
inputs[i].type = INPUTType.INPUT_KEYBOARD;
inputs[i].Event.ki.dwFlags = KEYEVENTF.SCANCODE;
inputs[i].Event.ki.wScan = (ushort)skey;
}
inputs[key.Length].type = INPUTType.INPUT_KEYBOARD;
inputs[key.Length].Event.ki.dwFlags = KEYEVENTF.UNICODE;
inputs[key.Length].Event.ki.dwFlags |= KEYEVENTF.KEYUP;
uint cSuccess = SendInput(inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
}
}

How to change screen resolution in WPF?

how can I change screen resolution programmatically in WPF?
And how can I get a list of available resolutions for display?
I tried have a go at the example described in this article: http://www.c-sharpcorner.com/UploadFile/Joshy_geo/changescreenresolution10102006112110AM/changescreenresolution.aspx But Screen and Resolution classes do not exist in the System.Diagnostics namespace.
C#/WPF
Thanx.
Sorry for the trouble. I found the solution for Windows Forms and made minor changes.
WPF Solution
Place two Listbox (listDevices and listSettings) and Button (btnSave) on the window.
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
public partial class MainWindow : Window
{
[StructLayout(LayoutKind.Sequential)]
public struct DISPLAY_DEVICE
{
public int cb;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string DeviceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceString;
public int StateFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceKey;
public DISPLAY_DEVICE(int flags)
{
cb = 0;
StateFlags = flags;
DeviceName = new string((char)32, 32);
DeviceString = new string((char)32, 128);
DeviceID = new string((char)32, 128);
DeviceKey = new string((char)32, 128);
cb = Marshal.SizeOf(this);
}
}
[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public short dmOrientation;
public short dmPaperSize;
public short dmPaperLength;
public short dmPaperWidth;
public short dmScale;
public short dmCopies;
public short dmDefaultSource;
public short dmPrintQuality;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmFormName;
public short dmUnusedPadding;
public short dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
}
public MainWindow()
{
InitializeComponent();
EnumDevices();
}
private void listDevices_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int devNum = listDevices.SelectedIndex;
bool isMain = MainDevice(devNum);
btnSet.IsEnabled = isMain; // enable only for the main device
EnumModes(devNum);
}
private void btnSet_Click(object sender, RoutedEventArgs e)
{ //set selected display mode
int devNum = listDevices.SelectedIndex;
int modeNum = listSettings.SelectedIndex;
DEVMODE d = GetDevmode(devNum, modeNum);
if (d.dmBitsPerPel != 0 & d.dmPelsWidth != 0
& d.dmPelsHeight != 0)
{
ChangeDisplaySettings(ref d, 0);
}
}
private void EnumModes(int devNum)
{
listSettings.Items.Clear();
string devName = GetDeviceName(devNum);
DEVMODE devMode = new DEVMODE();
int modeNum = 0;
bool result = true;
do
{
result = EnumDisplaySettings(devName,
modeNum, ref devMode);
if (result)
{
string item = DevmodeToString(devMode);
listSettings.Items.Add(item);
}
modeNum++;
} while (result);
if (listSettings.Items.Count > 0)
{
DEVMODE current = GetDevmode(devNum, -1);
// int selected = listSettings.FindString(DevmodeToString(current));
int selected = listSettings.Items.IndexOf(DevmodeToString(current));
if (selected >= 0)
{
listSettings.SelectedIndex = selected;
// listSettings.SetSelected(selected, true);
}
}
}
private DEVMODE GetDevmode(int devNum, int modeNum)
{ //populates DEVMODE for the specified device and mode
DEVMODE devMode = new DEVMODE();
string devName = GetDeviceName(devNum);
EnumDisplaySettings(devName, modeNum, ref devMode);
return devMode;
}
private string DevmodeToString(DEVMODE devMode)
{
return devMode.dmPelsWidth.ToString() +
" x " + devMode.dmPelsHeight.ToString() +
", " + devMode.dmBitsPerPel.ToString() +
" bits, " +
devMode.dmDisplayFrequency.ToString() + " Hz";
}
private void EnumDevices()
{ //populates Display Devices list
this.listDevices.Items.Clear();
DISPLAY_DEVICE d = new DISPLAY_DEVICE(0);
int devNum = 0;
bool result;
do
{
result = EnumDisplayDevices(IntPtr.Zero,
devNum, ref d, 0);
if (result)
{
string item = devNum.ToString() +
". " + d.DeviceString.Trim();
if ((d.StateFlags & 4) != 0) item += " - main";
this.listDevices.Items.Add(item);
}
devNum++;
} while (result);
}
private string GetDeviceName(int devNum)
{
DISPLAY_DEVICE d = new DISPLAY_DEVICE(0);
bool result = EnumDisplayDevices(IntPtr.Zero,
devNum, ref d, 0);
return (result ? d.DeviceName.Trim() : "#error#");
}
private bool MainDevice(int devNum)
{ //whether the specified device is the main device
DISPLAY_DEVICE d = new DISPLAY_DEVICE(0);
if (EnumDisplayDevices(IntPtr.Zero, devNum, ref d, 0))
{
return ((d.StateFlags & 4) != 0);
} return false;
}
[DllImport("User32.dll")]
private static extern bool EnumDisplayDevices(
IntPtr lpDevice, int iDevNum,
ref DISPLAY_DEVICE lpDisplayDevice, int dwFlags);
[DllImport("User32.dll")]
private static extern bool EnumDisplaySettings(
string devName, int modeNum, ref DEVMODE devMode);
[DllImport("user32.dll")]
public static extern int ChangeDisplaySettings(
ref DEVMODE devMode, int flags);
}
WPF Screen Resolution Source: http://www.mediafire.com/?ciiymhmc7v28v4y

Categories