root/Apple Wireless Keyboard Helper/trunk/Misuzilla.Applications.AppleWirelessKeyboardHelper/Util.cs

Revision 320, 3.6 kB (checked in by tomoyo, 15 months ago)

Apple Wireless Keyboard Helperを追加。

  • Property svn:keywords set to Id
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Windows.Forms;
5using System.Diagnostics;
6using System.Runtime.InteropServices;
7using System.Diagnostics.CodeAnalysis;
8using System.Security;
9using System.Security.Permissions;
10using Microsoft.Win32.SafeHandles;
11using System.Threading;
12using System.IO;
13using Microsoft.Scripting;
14
15namespace 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}
Note: See TracBrowser for help on using the browser.