程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 整理分享C#通過user32.dll模擬物理按鍵操作的代碼,

整理分享C#通過user32.dll模擬物理按鍵操作的代碼,

編輯:C#入門知識

整理分享C#通過user32.dll模擬物理按鍵操作的代碼,


對系統模擬按鍵方面的知識和按鍵映射代碼做了一下梳理,在這裡分享出來,適用於開發自動操作工具和游戲外掛。

主代碼:

public const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key click flag
        public const int KEYEVENTF_KEYUP = 0x0002; //Key up flag
        [DllImport("user32.dll")]
        private static extern void keybd_event(byte bVk, byte bSCan, int dwFlags, int dwExtraInfo);
        [DllImport("user32.dll")]
        private static extern byte MapVirtualKey(byte wCode, int wMap);

        public static void 模擬按下按鍵(VirtualKeyCode 虛擬按鍵代碼)
        {
            var code = (byte)虛擬按鍵代碼;
            keybd_event(code, 0, 0, 0);
        }

        public static void 模擬彈起按鍵(VirtualKeyCode 虛擬按鍵代碼)
        {
            var code = (byte) 虛擬按鍵代碼;
            keybd_event(code, 0, KEYEVENTF_KEYUP, 0);
        }

        public static void 模擬單擊按鍵(VirtualKeyCode 虛擬按鍵代碼)
        {
            var code = (byte)虛擬按鍵代碼;
            keybd_event(code, 0, KEYEVENTF_EXTENDEDKEY, 0);
        }

網上關於keybd_event的dwFlags參數功能說法很混亂,經我測試貌似是KEYEVENTF_EXTENDEDKEY表示一次單擊,0表示按下,KEYEVENTF_KEYUP表示彈起,不一定完全正確,希望高人指點一下。

另外MapVirtualKey的作用實在不懂,所以就沒用上,看到有人這麼調用不知有什麼區別:

var code = (byte)虛擬按鍵代碼;
            keybd_event(code, MapVirtualKey(code,0), 0, 0);

我試過好像也沒什麼變化~到底MapVirtualKey是干什麼用的呢??

VirtualKeyCode枚舉:

/// <summary>
    /// 虛擬按鍵代碼
    /// 參考於 http://msdn.microsoft.com/zh-cn/library/dd375731(v=vs.85).aspx
    /// </summary>
    public enum VirtualKeyCode
    {
        /// <summary>
        /// Left mouse button
        /// </summary>
        Left_mouse_button = 0x01,
        /// <summary>
        /// Right mouse button
        /// </summary>
        Right_mouse_button = 0x02,
        /// <summary>
        /// Control-break processing
        /// </summary>
        Control_break_processing = 0x03,
        /// <summary>
        /// Middle mouse button (three-button mouse)
        /// </summary>
        Middle_mouse_button = 0x04,
        /// <summary>
        /// X1 mouse button
        /// </summary>
        X1_mouse_button = 0x05,
        /// <summary>
        /// X2 mouse button
        /// </summary>
        X2_mouse_button = 0x06,
        /// <summary>
        /// Undefined
        /// </summary>
        Undefined1 = 0x07,
        /// <summary>
        /// BACKSPACE key
        /// </summary>
        BACKSPACE_key = 0x08,
        /// <summary>
        /// TAB key
        /// </summary>
        TAB_key = 0x09,
        /// <summary>
        /// CLEAR key
        /// </summary>
        CLEAR_key = 0x0C,
        /// <summary>
        /// ENTER key
        /// </summary>
        ENTER_key = 0x0D,
        /// <summary>
        /// SHIFT key
        /// </summary>
        SHIFT_key = 0x10,
        /// <summary>
        /// CTRL key
        /// </summary>
        CTRL_key = 0x11,
        /// <summary>
        /// ALT key
        /// </summary>
        ALT_key = 0x12,
        /// <summary>
        /// PAUSE key
        /// </summary>
        PAUSE_key = 0x13,
        /// <summary>
        /// CAPS LOCK key
        /// </summary>
        CAPS_LOCK_key = 0x14,
        /// <summary>
        /// IME Kana mode
        /// </summary>
        IME_Kana_mode = 0x15,
        /// <summary>
        /// IME Hanguel mode (maintained for compatibility; use VK_HANGUL)
        /// </summary>
        IME_Hanguel_mode = 0x15,
        /// <summary>
        /// IME Hangul mode
        /// </summary>
        IME_Hangul_mode = 0x15,
        /// <summary>
        /// Undefined
        /// </summary>
        Undefined2 = 0x16,
        /// <summary>
        /// IME Junja mode
        /// </summary>
        IME_Junja_mode = 0x17,
        /// <summary>
        /// IME final mode
        /// </summary>
        IME_final_mode = 0x18,
        /// <summary>
        /// IME Hanja mode
        /// </summary>
        IME_Hanja_mode = 0x19,
        /// <summary>
        /// IME Kanji mode
        /// </summary>
        IME_Kanji_mode = 0x19,
        /// <summary>
        /// Undefined
        /// </summary>
        Undefined = 0x1A,
        /// <summary>
        /// ESC key
        /// </summary>
        ESC_key = 0x1B,
        /// <summary>
        /// IME convert
        /// </summary>
        IME_convert = 0x1C,
        /// <summary>
        /// IME nonconvert
        /// </summary>
        IME_nonconvert = 0x1D,
        /// <summary>
        /// IME accept
        /// </summary>
        IME_accept = 0x1E,
        /// <summary>
        /// IME mode change request
        /// </summary>
        IME_mode_change_request = 0x1F,
        /// <summary>
        /// SPACEBAR
        /// </summary>
        SPACEBAR = 0x20,
        /// <summary>
        /// PAGE UP key
        /// </summary>
        PAGE_UP_key = 0x21,
        /// <summary>
        /// PAGE DOWN key
        /// </summary>
        PAGE_DOWN_key = 0x22,
        /// <summary>
        /// END key
        /// </summary>
        END_key = 0x23,
        /// <summary>
        /// HOME key
        /// </summary>
        HOME_key = 0x24,
        /// <summary>
        /// LEFT ARROW key
        /// </summary>
        LEFT_ARROW_key = 0x25,
        /// <summary>
        /// UP ARROW key
        /// </summary>
        UP_ARROW_key = 0x26,
        /// <summary>
        /// RIGHT ARROW key
        /// </summary>
        RIGHT_ARROW_key = 0x27,
        /// <summary>
        /// DOWN ARROW key
        /// </summary>
        DOWN_ARROW_key = 0x28,
        /// <summary>
        /// SELECT key
        /// </summary>
        SELECT_key = 0x29,
        /// <summary>
        /// PRINT key
        /// </summary>
        PRINT_key = 0x2A,
        /// <summary>
        /// EXECUTE key
        /// </summary>
        EXECUTE_key = 0x2B,
        /// <summary>
        /// PRINT SCREEN key
        /// </summary>
        PRINT_SCREEN_key = 0x2C,
        /// <summary>
        /// INS key
        /// </summary>
        INS_key = 0x2D,
        /// <summary>
        /// DEL key
        /// </summary>
        DEL_key = 0x2E,
        /// <summary>
        /// HELP key
        /// </summary>
        HELP_key = 0x2F,
        /// <summary>
        /// 0 key
        /// </summary>
        _0_key = 0x30,
        /// <summary>
        /// 1 key
        /// </summary>
        _1_key = 0x31,
        /// <summary>
        /// 2 key
        /// </summary>
        _2_key = 0x32,
        /// <summary>
        /// 3 key
        /// </summary>
        _3_key = 0x33,
        /// <summary>
        /// 4 key
        /// </summary>
        _4_key = 0x34,
        /// <summary>
        /// 5 key
        /// </summary>
        _5_key = 0x35,
        /// <summary>
        /// 6 key
        /// </summary>
        _6_key = 0x36,
        /// <summary>
        /// 7 key
        /// </summary>
        _7_key = 0x37,
        /// <summary>
        /// 8 key
        /// </summary>
        _8_key = 0x38,
        /// <summary>
        /// 9 key
        /// </summary>
        _9_key = 0x39,
        /// <summary>
        /// A key
        /// </summary>
        A_key = 0x41,
        /// <summary>
        /// B key
        /// </summary>
        B_key = 0x42,
        /// <summary>
        /// C key
        /// </summary>
        C_key = 0x43,
        /// <summary>
        /// D key
        /// </summary>
        D_key = 0x44,
        /// <summary>
        /// E key
        /// </summary>
        E_key = 0x45,
        /// <summary>
        /// F key
        /// </summary>
        F_key = 0x46,
        /// <summary>
        /// G key
        /// </summary>
        G_key = 0x47,
        /// <summary>
        /// H key
        /// </summary>
        H_key = 0x48,
        /// <summary>
        /// I key
        /// </summary>
        I_key = 0x49,
        /// <summary>
        /// J key
        /// </summary>
        J_key = 0x4A,
        /// <summary>
        /// K key
        /// </summary>
        K_key = 0x4B,
        /// <summary>
        /// L key
        /// </summary>
        L_key = 0x4C,
        /// <summary>
        /// M key
        /// </summary>
        M_key = 0x4D,
        /// <summary>
        /// N key
        /// </summary>
        N_key = 0x4E,
        /// <summary>
        /// O key
        /// </summary>
        O_key = 0x4F,
        /// <summary>
        /// P key
        /// </summary>
        P_key = 0x50,
        /// <summary>
        /// Q key
        /// </summary>
        Q_key = 0x51,
        /// <summary>
        /// R key
        /// </summary>
        R_key = 0x52,
        /// <summary>
        /// S key
        /// </summary>
        S_key = 0x53,
        /// <summary>
        /// T key
        /// </summary>
        T_key = 0x54,
        /// <summary>
        /// U key
        /// </summary>
        U_key = 0x55,
        /// <summary>
        /// V key
        /// </summary>
        V_key = 0x56,
        /// <summary>
        /// W key
        /// </summary>
        W_key = 0x57,
        /// <summary>
        /// X key
        /// </summary>
        X_key = 0x58,
        /// <summary>
        /// Y key
        /// </summary>
        Y_key = 0x59,
        /// <summary>
        /// Z key
        /// </summary>
        Z_key = 0x5A,
        /// <summary>
        /// Left Windows key (Natural keyboard)
        /// </summary>
        Left_Windows_key = 0x5B,
        /// <summary>
        /// Right Windows key (Natural keyboard)
        /// </summary>
        Right_Windows_key = 0x5C,
        /// <summary>
        /// Applications key (Natural keyboard)
        /// </summary>
        Applications_key = 0x5D,
        /// <summary>
        /// Reserved
        /// </summary>
        Reserved1 = 0x5E,
        /// <summary>
        /// Computer Sleep key
        /// </summary>
        Computer_Sleep_key = 0x5F,
        /// <summary>
        /// Numeric keypad 0 key
        /// </summary>
        Numeric_keypad_0_key = 0x60,
        /// <summary>
        /// Numeric keypad 1 key
        /// </summary>
        Numeric_keypad_1_key = 0x61,
        /// <summary>
        /// Numeric keypad 2 key
        /// </summary>
        Numeric_keypad_2_key = 0x62,
        /// <summary>
        /// Numeric keypad 3 key
        /// </summary>
        Numeric_keypad_3_key = 0x63,
        /// <summary>
        /// Numeric keypad 4 key
        /// </summary>
        Numeric_keypad_4_key = 0x64,
        /// <summary>
        /// Numeric keypad 5 key
        /// </summary>
        Numeric_keypad_5_key = 0x65,
        /// <summary>
        /// Numeric keypad 6 key
        /// </summary>
        Numeric_keypad_6_key = 0x66,
        /// <summary>
        /// Numeric keypad 7 key
        /// </summary>
        Numeric_keypad_7_key = 0x67,
        /// <summary>
        /// Numeric keypad 8 key
        /// </summary>
        Numeric_keypad_8_key = 0x68,
        /// <summary>
        /// Numeric keypad 9 key
        /// </summary>
        Numeric_keypad_9_key = 0x69,
        /// <summary>
        /// Multiply key
        /// </summary>
        Multiply_key = 0x6A,
        /// <summary>
        /// Add key
        /// </summary>
        Add_key = 0x6B,
        /// <summary>
        /// Separator key
        /// </summary>
        Separator_key = 0x6C,
        /// <summary>
        /// Subtract key
        /// </summary>
        Subtract_key = 0x6D,
        /// <summary>
        /// Decimal key
        /// </summary>
        Decimal_key = 0x6E,
        /// <summary>
        /// Divide key
        /// </summary>
        Divide_key = 0x6F,
        /// <summary>
        /// F1 key
        /// </summary>
        F1_key = 0x70,
        /// <summary>
        /// F2 key
        /// </summary>
        F2_key = 0x71,
        /// <summary>
        /// F3 key
        /// </summary>
        F3_key = 0x72,
        /// <summary>
        /// F4 key
        /// </summary>
        F4_key = 0x73,
        /// <summary>
        /// F5 key
        /// </summary>
        F5_key = 0x74,
        /// <summary>
        /// F6 key
        /// </summary>
        F6_key = 0x75,
        /// <summary>
        /// F7 key
        /// </summary>
        F7_key = 0x76,
        /// <summary>
        /// F8 key
        /// </summary>
        F8_key = 0x77,
        /// <summary>
        /// F9 key
        /// </summary>
        F9_key = 0x78,
        /// <summary>
        /// F10 key
        /// </summary>
        F10_key = 0x79,
        /// <summary>
        /// F11 key
        /// </summary>
        F11_key = 0x7A,
        /// <summary>
        /// F12 key
        /// </summary>
        F12_key = 0x7B,
        /// <summary>
        /// F13 key
        /// </summary>
        F13_key = 0x7C,
        /// <summary>
        /// F14 key
        /// </summary>
        F14_key = 0x7D,
        /// <summary>
        /// F15 key
        /// </summary>
        F15_key = 0x7E,
        /// <summary>
        /// F16 key
        /// </summary>
        F16_key = 0x7F,
        /// <summary>
        /// F17 key
        /// </summary>
        F17_key = 0x80,
        /// <summary>
        /// F18 key
        /// </summary>
        F18_key = 0x81,
        /// <summary>
        /// F19 key
        /// </summary>
        F19_key = 0x82,
        /// <summary>
        /// F20 key
        /// </summary>
        F20_key = 0x83,
        /// <summary>
        /// F21 key
        /// </summary>
        F21_key = 0x84,
        /// <summary>
        /// F22 key
        /// </summary>
        F22_key = 0x85,
        /// <summary>
        /// F23 key
        /// </summary>
        F23_key = 0x86,
        /// <summary>
        /// F24 key
        /// </summary>
        F24_key = 0x87,
        /// <summary>
        /// NUM LOCK key
        /// </summary>
        NUM_LOCK_key = 0x90,
        /// <summary>
        /// SCROLL LOCK key
        /// </summary>
        SCROLL_LOCK_key = 0x91,
        /// <summary>
        /// Left SHIFT key
        /// </summary>
        Left_SHIFT_key = 0xA0,
        /// <summary>
        /// Right SHIFT key
        /// </summary>
        Right_SHIFT_key = 0xA1,
        /// <summary>
        /// Left CONTROL key
        /// </summary>
        Left_CONTROL_key = 0xA2,
        /// <summary>
        /// Right CONTROL key
        /// </summary>
        Right_CONTROL_key = 0xA3,
        /// <summary>
        /// Left MENU key
        /// </summary>
        Left_MENU_key = 0xA4,
        /// <summary>
        /// Right MENU key
        /// </summary>
        Right_MENU_key = 0xA5,
        /// <summary>
        /// Browser Back key
        /// </summary>
        Browser_Back_key = 0xA6,
        /// <summary>
        /// Browser Forward key
        /// </summary>
        Browser_Forward_key = 0xA7,
        /// <summary>
        /// Browser Refresh key
        /// </summary>
        Browser_Refresh_key = 0xA8,
        /// <summary>
        /// Browser Stop key
        /// </summary>
        Browser_Stop_key = 0xA9,
        /// <summary>
        /// Browser Search key
        /// </summary>
        Browser_Search_key = 0xAA,
        /// <summary>
        /// Browser Favorites key
        /// </summary>
        Browser_Favorites_key = 0xAB,
        /// <summary>
        /// Browser Start and Home key
        /// </summary>
        Browser_Start_and_Home_key = 0xAC,
        /// <summary>
        /// Volume Mute key
        /// </summary>
        Volume_Mute_key = 0xAD,
        /// <summary>
        /// Volume Down key
        /// </summary>
        Volume_Down_key = 0xAE,
        /// <summary>
        /// Volume Up key
        /// </summary>
        Volume_Up_key = 0xAF,
        /// <summary>
        /// Next Track key
        /// </summary>
        Next_Track_key = 0xB0,
        /// <summary>
        /// Previous Track key
        /// </summary>
        Previous_Track_key = 0xB1,
        /// <summary>
        /// Stop Media key
        /// </summary>
        Stop_Media_key = 0xB2,
        /// <summary>
        /// Play/Pause Media key
        /// </summary>
        Play_Or_Pause_Media_key = 0xB3,
        /// <summary>
        /// Start Mail key
        /// </summary>
        Start_Mail_key = 0xB4,
        /// <summary>
        /// Select Media key
        /// </summary>
        Select_Media_key = 0xB5,
        /// <summary>
        /// Start Application 1 key
        /// </summary>
        Start_Application_1_key = 0xB6,
        /// <summary>
        /// Start Application 2 key
        /// </summary>
        Start_Application_2_key = 0xB7,
        /// <summary>
        /// Used for miscellaneous characters; it can vary by keyboard.
        /// </summary>
        Used_for_miscellaneous_characters1 = 0xBA,
        /// <summary>
        /// Used for miscellaneous characters; it can vary by keyboard.
        /// </summary>
        Used_for_miscellaneous_characters2 = 0xBF,
        /// <summary>
        /// Used for miscellaneous characters; it can vary by keyboard.
        /// </summary>
        Used_for_miscellaneous_characters3 = 0xC0,
        /// <summary>
        /// Used for miscellaneous characters; it can vary by keyboard.
        /// </summary>
        Used_for_miscellaneous_characters4 = 0xDB,
        /// <summary>
        /// Used for miscellaneous characters; it can vary by keyboard.
        /// </summary>
        Used_for_miscellaneous_characters5 = 0xDC,
        /// <summary>
        /// Used for miscellaneous characters; it can vary by keyboard.
        /// </summary>
        Used_for_miscellaneous_characters6 = 0xDD,
        /// <summary>
        /// Used for miscellaneous characters; it can vary by keyboard.
        /// </summary>
        Used_for_miscellaneous_characters7 = 0xDE,
        /// <summary>
        /// Used for miscellaneous characters; it can vary by keyboard.
        /// </summary>
        Used_for_miscellaneous_characters8 = 0xDF,
        /// <summary>
        /// Reserved
        /// </summary>
        Reserved2 = 0xE0,
        /// <summary>
        /// OEM specific
        /// </summary>
        OEM_specific1 = 0xE1,
        /// <summary>
        /// Either the angle bracket key or the backslash key on the RT 102-key keyboard
        /// </summary>
        Either_the_angle_bracket_key_or_the_backslash_key_on_the_RT_102_key_keyboard = 0xE2,
        /// <summary>
        /// IME PROCESS key
        /// </summary>
        IME_PROCESS_key = 0xE5,
        /// <summary>
        /// OEM specific
        /// </summary>
        OEM_specific2 = 0xE6,
        /// <summary>
        /// Used to pass Unicode characters as if they were keystrokes. The VK_PACKET key is the low word of a 32-bit Virtual Key value used for non-keyboard input methods. For more information, see Remark in KEYBDINPUT, SendInput, WM_KEYDOWN, and WM_KEYUP
        /// </summary>
        Used_to_pass_Unicode_characters_as_if_they_were_keystrokes = 0xE7,
        /// <summary>
        /// Unassigned
        /// </summary>
        Unassigned = 0xE8,
        /// <summary>
        /// Attn key
        /// </summary>
        Attn_key = 0xF6,
        /// <summary>
        /// CrSel key
        /// </summary>
        CrSel_key = 0xF7,
        /// <summary>
        /// ExSel key
        /// </summary>
        ExSel_key = 0xF8,
        /// <summary>
        /// Erase EOF key
        /// </summary>
        Erase_EOF_key = 0xF9,
        /// <summary>
        /// Play key
        /// </summary>
        Play_key = 0xFA,
        /// <summary>
        /// Zoom key
        /// </summary>
        Zoom_key = 0xFB,
        /// <summary>
        /// Reserved
        /// </summary>
        Reserved = 0xFC,
        /// <summary>
        /// PA1 key
        /// </summary>
        PA1_key = 0xFD,
        /// <summary>
        /// Clear key
        /// </summary>
        Clear_key = 0xFE
    }

調用演示:

//模擬實現Ctrl+O操作
模擬按下按鍵(VirtualKeyCode.CTRL_key)
模擬單擊按鍵(VirtualKeyCode.O_key)
模擬彈起按鍵(VirtualKeyCode.CTRL_key)

怎整理C 盤??

找回c盤空間:
1.關閉系統還原:我的電腦屬性/系統還原/關閉所有磁盤上的系統還原,但是以後就不能用系統還原了!
2.關閉系統休眠:控制面板/電源/休眠/在啟動系統休眠前面的勾去掉
3.移動虛擬內存,我的電腦屬性/高級/性能/設置/高級/更改/選C盤也就是系統盤,選無分頁面,然後把虛擬內存設置到其磁盤,要剩余磁盤空間多的磁盤,比如D,E,F等盤. 設成內存的1.5~2.5倍,大小可設成一樣!
5.清理IE臨時文件夾,internet選項,刪除臨時文件和脫機文件
6.刪除系統日志和程序日志,我的電腦/控制面板/管理工具/計算機管理/事件查看器/應用程序,鼠標右鍵/清除所事件,在依次清除系統日志
7.清理系統緩存:2000系統是:C:\WINNT\system32\dllcache下的所有文件
XP系統是:C:\windows\system32\dllcache下的所有文件 清理系統緩存(打開我的電腦/工具/文件和文件夾選項/隱藏受保護的系統文件的勾去掉在把顯示全部文件勾上)。也可以直接運行sfc.exe /purgecache命令自動刪除。
8.清空回收站
9.刪除c:\windows\SoftwareDistribution\Download下的文件(系統更新時下載的文件如你裝好了更新也就沒有用了)
10.刪除c:\windows\RegisteredPackages下所有目錄
11.刪除C:\WINDOWS\Downloaded Program Files下所有的文件
12.我的電腦 文件夾選項 查看 隱藏已知受系統保護的文件勾去掉,顯示所有文件勾上確定。 刪除c:\windows\所有帶$的文件(系統更新後的備份文件)
13.用優化大師清理C盤垃圾和注冊表垃圾 (優化大師相對還是比較安全的,掃描到的全部刪除就行了!)
14.中了毒(蠕蟲)會不斷復制垃圾文件,占用大量的系統盤空間,導致系統癱瘓,建議用主流殺毒軟件升級到最新病毒庫進行查
 

【單選題-1】 信息處理一般包括獲取、存儲、( )、傳遞 A提取整理 B共享公開 C加工 A B C

選C,我們會考剛做的嘿嘿
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved