| 1 | using System; |
|---|
| 2 | using System.Collections.Generic; |
|---|
| 3 | using System.Text; |
|---|
| 4 | using System.Windows.Forms; |
|---|
| 5 | using System.Diagnostics; |
|---|
| 6 | using System.Runtime.InteropServices; |
|---|
| 7 | using System.Diagnostics.CodeAnalysis; |
|---|
| 8 | using System.Security; |
|---|
| 9 | using System.Security.Permissions; |
|---|
| 10 | using Microsoft.Win32.SafeHandles; |
|---|
| 11 | using System.Threading; |
|---|
| 12 | using System.IO; |
|---|
| 13 | using Microsoft.Scripting; |
|---|
| 14 | |
|---|
| 15 | namespace Misuzilla.Applications.AppleWirelessKeyboardHelper |
|---|
| 16 | { |
|---|
| 17 | [SuppressMessage("Microsoft.Naming", "CA1724")] // CA1724: System.Web.Util �Ƃ��Ԃ� [SecurityPermission(SecurityAction.LinkDemand)] |
|---|
| 18 | public static class Util |
|---|
| 19 | { |
|---|
| 20 | internal readonly static IntPtr InputExtraInfoValue = (IntPtr)0x37564; |
|---|
| 21 | |
|---|
| 22 | /// <summary> |
|---|
| 23 | /// ���݃t�H�[�J�X��E�B���h�E�ɑ��Ďw�肵���L�[��p/Down�𑗐M���܂��B |
|---|
| 24 | /// </summary> |
|---|
| 25 | /// <param name="keyInputs"></param> |
|---|
| 26 | public static UInt32 SendInput(params Keys[] keyInputs) |
|---|
| 27 | { |
|---|
| 28 | List<Win32.INPUT> inputs = new List<Win32.INPUT>(); |
|---|
| 29 | foreach (Keys key in keyInputs) |
|---|
| 30 | { |
|---|
| 31 | Debug.WriteLine("SendInput: " + key.ToString()); |
|---|
| 32 | Win32.INPUT input = new Win32.INPUT(); |
|---|
| 33 | input.ki = new Win32.KEYBDINPUT(); |
|---|
| 34 | input.type = Win32.INPUT_KEYBOARD; |
|---|
| 35 | input.ki.wScan = 0; |
|---|
| 36 | input.ki.wVk = (Byte)key; |
|---|
| 37 | input.ki.dwFlags = 0; // KEYDOWN |
|---|
| 38 | input.ki.dwExtraInfo = InputExtraInfoValue; |
|---|
| 39 | inputs.Add(input); |
|---|
| 40 | |
|---|
| 41 | input = new Win32.INPUT(); |
|---|
| 42 | input.ki = new Win32.KEYBDINPUT(); |
|---|
| 43 | input.type = Win32.INPUT_KEYBOARD; |
|---|
| 44 | input.ki.wScan = 0; |
|---|
| 45 | input.ki.wVk = (Byte)key; |
|---|
| 46 | input.ki.dwFlags = Win32.KEYEVENTF_KEYUP; // KEYUP |
|---|
| 47 | input.ki.dwExtraInfo = InputExtraInfoValue; |
|---|
| 48 | inputs.Add(input); |
|---|
| 49 | } |
|---|
| 50 | Win32.INPUT[] inputsArr = inputs.ToArray(); |
|---|
| 51 | |
|---|
| 52 | return Win32.SendInput((UInt32)inputsArr.Length, inputsArr, Marshal.SizeOf(typeof(Win32.INPUT))); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | /// <summary> |
|---|
| 56 | /// |
|---|
| 57 | /// </summary> |
|---|
| 58 | /// <param name="isUp"></param> |
|---|
| 59 | /// <param name="keyInput"></param> |
|---|
| 60 | public static UInt32 SendInput(Boolean isUp, Keys key) |
|---|
| 61 | { |
|---|
| 62 | Debug.WriteLine("SendInput: " + key.ToString() + " ( " + (isUp ? "Up" : "Down" ) + ")"); |
|---|
| 63 | Win32.INPUT input = new Win32.INPUT(); |
|---|
| 64 | input.ki = new Win32.KEYBDINPUT(); |
|---|
| 65 | input.type = Win32.INPUT_KEYBOARD; |
|---|
| 66 | input.ki.wScan = 0; |
|---|
| 67 | input.ki.wVk = (Byte)key; |
|---|
| 68 | input.ki.dwFlags = (UInt16)(isUp ? Win32.KEYEVENTF_KEYUP : 0); |
|---|
| 69 | input.ki.dwExtraInfo = InputExtraInfoValue; |
|---|
| 70 | |
|---|
| 71 | Win32.INPUT[] inputsArr = new Win32.INPUT[1]; |
|---|
| 72 | inputsArr[0] = input; |
|---|
| 73 | |
|---|
| 74 | return Win32.SendInput((UInt32)inputsArr.Length, inputsArr, Marshal.SizeOf(typeof(Win32.INPUT))); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | /// <summary> |
|---|
| 78 | /// |
|---|
| 79 | /// </summary> |
|---|
| 80 | /// <param name="drive"></param> |
|---|
| 81 | public static void Eject(String drive) |
|---|
| 82 | { |
|---|
| 83 | using (SafeFileHandle hDevice = Win32.CreateFile(String.Format(@"\\.\{0}:", drive), FileAccess.Read, FileShare.Read, IntPtr.Zero, FileMode.Open, Win32.EFileAttributes.Normal, IntPtr.Zero)) |
|---|
| 84 | { |
|---|
| 85 | UInt32 retVal = 0; |
|---|
| 86 | NativeOverlapped retOverlapped = new NativeOverlapped(); |
|---|
| 87 | Win32.DeviceIoControl(hDevice, Win32.EIOControlCode.StorageEjectMedia, null, 0, null, 0, ref retVal, ref retOverlapped); |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | } |
|---|