keeweb/helper/win32/src/KeeWebHelper/InputCommands/SendKeyCommand.cs

50 lines
1.5 KiB
C#
Raw Normal View History

2016-04-24 14:12:33 +02:00
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace KeeWebHelper.InputCommands
{
class SendKeyCommand : InputCommandBase
{
public byte Key { get; set; }
public ModifierKeys Modifiers { get; set; }
public SendKeyCommand(byte key, ModifierKeys modifiers)
{
Key = key;
Modifiers = modifiers;
}
public override void Execute()
{
2016-08-19 19:30:11 +02:00
InputStateValidator.EnsureNoKeyIsPressed();
2016-04-24 14:12:33 +02:00
if ((Modifiers & ModifierKeys.Ctrl) != 0) { Down((byte)Keys.ControlKey); }
if ((Modifiers & ModifierKeys.Shift) != 0) { Down((byte)Keys.ShiftKey); }
if ((Modifiers & ModifierKeys.Alt) != 0) { Down((byte)Keys.Menu); }
Press(Key);
2016-04-24 14:23:58 +02:00
if ((Modifiers & ModifierKeys.Alt) != 0) { Up((byte)Keys.Menu); }
if ((Modifiers & ModifierKeys.Shift) != 0) { Up((byte)Keys.ShiftKey); }
if ((Modifiers & ModifierKeys.Ctrl) != 0) { Up((byte)Keys.ControlKey); }
2016-04-24 14:12:33 +02:00
}
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
public static void Down(byte code)
{
keybd_event(code, 0, 1, UIntPtr.Zero);
}
public static void Up(byte code)
{
keybd_event(code, 0, 3, UIntPtr.Zero);
}
public static void Press(byte code)
{
Down(code);
Up(code);
}
}
}