亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#實現(xiàn)虛擬鍵盤的實例詳解

 更新時間:2022年12月16日 10:00:17   作者:芝麻粒兒  
這篇文章主要為大家詳細介紹了如何利用C#實現(xiàn)虛擬鍵盤,文中的示例代碼講解詳細,對我們學習C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下

實踐過程

效果

代碼

public partial class Form1 : Form
{
    private HookEx.UserActivityHook hook;//實例化HookEx.UserActivityHook
    private static IDictionary<short, IList<FecitButton>> spacialVKButtonsMap;//表示鍵/值對應的泛型集合
    private static IDictionary<short, IList<FecitButton>> combinationVKButtonsMap;//表示鍵/值對應的泛型集合

    public Form1()
    {
        InitializeComponent();

        #region 將指定的按鈕值添加到鍵類型中
        spacialVKButtonsMap = new Dictionary<short, IList<FecitButton>>();
        combinationVKButtonsMap = new Dictionary<short, IList<FecitButton>>();
        IList<FecitButton> buttonList = new List<FecitButton>();//實例化IList<FecitButton>(按照索引單獨訪問的一組對象)
        buttonList.Add(this.btnLCTRL);//添加左面的CTRL鍵
        combinationVKButtonsMap.Add(KeyboardConstaint.VK_CONTROL, buttonList);//添加左面的CTRL鍵值
        buttonList = new List<FecitButton>();
        buttonList.Add(this.btnLSHFT);//添加左面的LSHFT鍵
        buttonList.Add(this.btnRSHFT);//添加右面的LSHFT鍵
        combinationVKButtonsMap.Add(KeyboardConstaint.VK_SHIFT, buttonList);//添加LSHFT鍵值
        buttonList = new List<FecitButton>();//實例化IList<FecitButton>
        buttonList.Add(this.btnLALT);//添加左面的ALT鍵
        combinationVKButtonsMap.Add(KeyboardConstaint.VK_MENU, buttonList);//添加左面的ALT鍵值
        buttonList = new List<FecitButton>();//實例化IList<FecitButton>
        buttonList.Add(this.btnLW);//添加左面的WIN鍵
        combinationVKButtonsMap.Add(KeyboardConstaint.VK_LWIN, buttonList);//添加左面的WIN鍵值
        buttonList = new List<FecitButton>();//實例化IList<FecitButton>
        buttonList.Add(this.btnLOCK);//添加LOCK鍵
        spacialVKButtonsMap.Add(KeyboardConstaint.VK_CAPITAL, buttonList);//添加LOCK鍵值
        #endregion

        foreach (Control ctrl in this.Controls)
        {
            FecitButton button = ctrl as FecitButton;
            if (button == null)
            {
                continue;
            }

            #region 設置按鍵的消息值
            short key = 0;//記錄鍵值
            bool isSpacialKey = true;
            //記錄快捷鍵的鍵值
            switch (button.Name)//獲取鍵名稱
            {
                case "btnLCTRL"://CTRL鍵的左鍵名稱
                case "btnRCTRL"://CTRL鍵的右鍵名稱
                    key = KeyboardConstaint.VK_CONTROL;//獲取CTRL鍵的鍵值
                    break;
                case "btnLSHFT"://SHFT鍵的左鍵名稱
                case "btnRSHFT"://SHFT鍵的左鍵名稱
                    key = KeyboardConstaint.VK_SHIFT;//獲取SHFT鍵的鍵值
                    break;
                case "btnLALT"://ALT鍵的左鍵名稱
                case "btnRALT"://ALT鍵的左鍵名稱
                    key = KeyboardConstaint.VK_MENU;//獲取ALT鍵的鍵值
                    break;
                case "btnLW"://WIN鍵的左鍵名稱
                case "btnRW"://WIN鍵的左鍵名稱
                    key = KeyboardConstaint.VK_LWIN;//獲取WIN鍵的鍵值
                    break;
                case "btnESC"://ESC鍵的名稱
                    key = KeyboardConstaint.VK_ESCAPE;//獲取ESC鍵的鍵值
                    break;
                case "btnTAB"://TAB鍵的名稱
                    key = KeyboardConstaint.VK_TAB;//獲取TAB鍵的鍵值
                    break;
                case "btnF1"://F1鍵的名稱
                    key = KeyboardConstaint.VK_F1;//獲取F1鍵的鍵值
                    break;
                case "btnF2":
                    key = KeyboardConstaint.VK_F2;
                    break;
                case "btnF3":
                    key = KeyboardConstaint.VK_F3;
                    break;
                case "btnF4":
                    key = KeyboardConstaint.VK_F4;
                    break;
                case "btnF5":
                    key = KeyboardConstaint.VK_F5;
                    break;
                case "btnF6":
                    key = KeyboardConstaint.VK_F6;
                    break;
                case "btnF7":
                    key = KeyboardConstaint.VK_F7;
                    break;

                case "btnF8":
                    key = KeyboardConstaint.VK_F8;
                    break;
                case "btnF9":
                    key = KeyboardConstaint.VK_F9;
                    break;
                case "btnF10":
                    key = KeyboardConstaint.VK_F10;
                    break;
                case "btnF11":
                    key = KeyboardConstaint.VK_F11;
                    break;
                case "btnF12":
                    key = KeyboardConstaint.VK_F12;
                    break;
                case "btnENT":
                case "btnNUMENT":
                    key = KeyboardConstaint.VK_RETURN;
                    break;
                case "btnWave":
                    key = KeyboardConstaint.VK_OEM_3;
                    break;
                case "btnSem":
                    key = KeyboardConstaint.VK_OEM_1;
                    break;
                case "btnQute":
                    key = KeyboardConstaint.VK_OEM_7;
                    break;
                case "btnSpace":
                    key = KeyboardConstaint.VK_SPACE;
                    break;
                case "btnBKSP":
                    key = KeyboardConstaint.VK_BACK;
                    break;
                case "btnComma":
                    key = KeyboardConstaint.VK_OEM_COMMA;
                    break;
                case "btnFullStop":
                    key = KeyboardConstaint.VK_OEM_PERIOD;
                    break;
                case "btnLOCK":
                    key = KeyboardConstaint.VK_CAPITAL;
                    break;
                case "btnMinus":
                    key = KeyboardConstaint.VK_OEM_MINUS;
                    break;
                case "btnEqual":
                    key = KeyboardConstaint.VK_OEM_PLUS;
                    break;
                case "btnLBracket":
                    key = KeyboardConstaint.VK_OEM_4;
                    break;
                case "btnRBracket":
                    key = KeyboardConstaint.VK_OEM_6;
                    break;
                case "btnPath":
                    key = KeyboardConstaint.VK_OEM_5;
                    break;
                case "btnDivide":
                    key = KeyboardConstaint.VK_OEM_2;
                    break;
                case "btnPSC":
                    key = KeyboardConstaint.VK_SNAPSHOT;
                    break;
                case "btnINS"://Insert鍵的名稱
                    key = KeyboardConstaint.VK_INSERT;//獲取Insert鍵的鍵值
                    break;
                case "btnDEL"://Delete鍵的名稱
                    key = KeyboardConstaint.VK_DELETE;//獲取Delete鍵的鍵值
                    break;
                default:
                    isSpacialKey = false;
                    break;
            }
            if (!isSpacialKey)
            {
                key = (short)button.Name[3];//獲取按鈕的鍵值
            }
            button.Tag = key;//在按鈕的Tag屬性中記錄相應的鍵值
            #endregion
            button.Click += ButtonOnClick;//重載按鈕的單擊事件
        }
        this.hook = new HookEx.UserActivityHook(true, true);
        HookEvents();
    }

    private void HookEvents()
    {
        this.hook.KeyDown += HookOnGlobalKeyDown;//重載hook類中的自定義事件KeyDown
        this.hook.KeyUp += HookOnGlobalKeyUp;//重載hook類中的自定義事件KeyUp
        this.hook.MouseActivity += HookOnMouseActivity;//重載hook類中的自定義事件MouseActivity
    }

    private void ButtonOnClick(object sender, EventArgs e)//按鍵的單擊事件
    {
        FecitButton btnKey = sender as FecitButton;//獲取當前按鍵的信息
        if (btnKey == null)//如果按鍵為空值
            return;
        SendKeyCommand(btnKey);//發(fā)送按鍵的信息
    }

    /// <summary>
    /// 接收并發(fā)送按鍵信息
    /// </summary>
    /// <param keyButton="FecitButton">按鍵信息</param>
    private void SendKeyCommand(FecitButton keyButton)
    {
        short key = Convert.ToInt16(keyButton.Tag.ToString());//獲取當前鍵的鍵值
        if (combinationVKButtonsMap.ContainsKey(key))//如果鍵值在鍵值列表中
        {
            if (keyButton.Checked)//如果按鈕處于按下狀態(tài)
            {
                SendKeyUp(key);//對按鈕進行抬起操作
            }
            else
            {
                SendKeyDown(key);//對按鈕進行按下操作
            }
        }
        else
        {
            //執(zhí)行按鈕按下和抬起的操作
            SendKeyDown(key);
            SendKeyUp(key);
        }
    }

    /// <summary>
    /// 記錄鍵盤的按下操作的值
    /// </summary>
    /// <param key="short">鍵值</param>
    private void SendKeyDown(short key)
    {
        Input[] input = new Input[1];//實例化Input[]
        input[0].type = INPUT.KEYBOARD;//記錄當有鍵值的類型
        input[0].ki.wVk = key;//記錄當前鍵值
        input[0].ki.time = NativeMethods.GetTickCount();//獲取自windows啟動以來經(jīng)歷的時間長度(毫秒)
        //消息的輸入
        if (NativeMethods.SendInput((uint)input.Length, input, Marshal.SizeOf(input[0])) < input.Length)
        {
            throw new Win32Exception(Marshal.GetLastWin32Error());//指定錯誤的初始化
        }
    }

    /// <summary>
    /// 記錄鍵盤抬起操作的值
    /// </summary>
    /// <param key="short">鍵值</param>
    private void SendKeyUp(short key)
    {
        Input[] input = new Input[1];//實例化Input[]
        input[0].type = INPUT.KEYBOARD;//記錄當有鍵值的類型
        input[0].ki.wVk = key;//記錄當前鍵值
        input[0].ki.dwFlags = KeyboardConstaint.KEYEVENTF_KEYUP;
        input[0].ki.time = NativeMethods.GetTickCount();//獲取自windows啟動以來經(jīng)歷的時間長度(毫秒)
        //消息的輸入
        if (NativeMethods.SendInput((uint)input.Length, input, Marshal.SizeOf(input[0])) < input.Length)
        {
            throw new Win32Exception(Marshal.GetLastWin32Error());//指定錯誤的初始化
        }
    }

    //鍵盤的按下事件
    private void HookOnGlobalKeyDown(object sender, HookEx.KeyExEventArgs e)
    {
        SetButtonStatus(e, true);
    }

    //鍵盤的抬起事件
    private void HookOnGlobalKeyUp(object sender, HookEx.KeyExEventArgs e)
    {
        SetButtonStatus(e, false);
    }

    /// <summary>
    /// 設置當前按鈕的狀態(tài)
    /// </summary>
    /// <param args="KeyExEventArgs">鍵信息</param>
    /// <param isDown="bool">標識,當前鍵是否按下</param>
    private void SetButtonStatus(HookEx.KeyExEventArgs args, bool isDown)
    {
        IList<FecitButton> buttonList = FindButtonList(args);//查找當有鍵
        if (buttonList.Count <= 0)//如果沒有找到
            return;//退出本次操作
        short key = (short)args.KeyValue;//獲取當前鍵的鍵值
        if (spacialVKButtonsMap.ContainsKey(key))//如果鍵/值列表中有該鍵
        {
            if (!isDown)//如果按鈕沒有被按下
            {
                FecitButton button = spacialVKButtonsMap[key][0];//設置按鈕的信息
                button.Checked = !button.Checked;//設置當前按鈕為按下狀態(tài)
            }
        }
        else
        {
            foreach (FecitButton button in buttonList)//遍歷IList中的所有按鈕
            {
                if (button == null)//如果按鈕為空
                    break;//退出循環(huán)
                button.Checked = isDown;//設置按鈕的狀態(tài)
            }
        }
    }

    /// <summary>
    /// 鼠標事件
    /// </summary>
    /// <param sener="object">鼠標對象</param>
    /// <param e="MouseExEventArgs">為MouseUp、MouseDown和MouseMove事件提供數(shù)據(jù)</param>
    private void HookOnMouseActivity(object sener, HookEx.MouseExEventArgs e)
    {
        Point location = e.Location;//獲取鼠標的位置
        if (e.Button == MouseButtons.Left)//如果是鼠標左鍵
        {
            Rectangle captionRect = new Rectangle(this.Location, new Size(this.Width, SystemInformation.CaptionHeight));//獲取窗體的所在區(qū)域
            if (captionRect.Contains(location))//如果鼠標在該窗體范圍內
            {   //設置窗體的擴展樣式
                NativeMethods.SetWindowLong(this.Handle, KeyboardConstaint.GWL_EXSTYLE, (int)NativeMethods.GetWindowLong(this.Handle, KeyboardConstaint.GWL_EXSTYLE) & (~KeyboardConstaint.WS_DISABLED));
                //將消息發(fā)送給指定窗體 
                NativeMethods.SendMessage(this.Handle, KeyboardConstaint.WM_SETFOCUS, IntPtr.Zero, IntPtr.Zero);
            }
            else
            {
                //設置窗體的擴展樣式
                NativeMethods.SetWindowLong(this.Handle, KeyboardConstaint.GWL_EXSTYLE, (int)NativeMethods.GetWindowLong(this.Handle, KeyboardConstaint.GWL_EXSTYLE) | KeyboardConstaint.WS_DISABLED);
            }
        }
    }

    /// <summary>
    /// 在鍵列表中查找鍵值
    /// </summary>
    /// <param args="KeyExEventArgs">鍵信息</param>
    private IList<FecitButton> FindButtonList(HookEx.KeyExEventArgs args)
    {
        short key = (short)args.KeyValue;//獲取鍵值
        if (key == KeyboardConstaint.VK_LCONTROL || key == KeyboardConstaint.VK_RCONTROL)//如果是CTRL鍵
        {
            key = KeyboardConstaint.VK_CONTROL;//記錄CTRL鍵值
        }
        else if (key == KeyboardConstaint.VK_LSHIFT || key == KeyboardConstaint.VK_RSHIFT)//如果是SHIFT鍵
        {
            key = KeyboardConstaint.VK_SHIFT;//記錄SHIFT鍵值
        }
        else if (key == KeyboardConstaint.VK_LMENU || key == KeyboardConstaint.VK_RMENU)//如果是ALT鍵
        {
            key = KeyboardConstaint.VK_MENU;//記錄ALT鍵值
        }
        else if (key == KeyboardConstaint.VK_RWIN)//如果是WIN鍵
        {
            key = KeyboardConstaint.VK_LWIN;//記錄WIN鍵值
        }

        if (combinationVKButtonsMap.ContainsKey(key))//如果在IDictionary的集合中
        {
            return combinationVKButtonsMap[key];//返回當前鍵的鍵值
        }
        IList<FecitButton> buttonList = new List<FecitButton>();//實例化IList<FecitButton>
        foreach (Control ctrl in this.Controls)//遍歷當前窗體中的所有控件
        {
            FecitButton button = ctrl as FecitButton;//如果當前控件是FecitButton按鈕
            if (button == null)//如果當前按鈕為空
                continue;//重新循環(huán)
            short theKey = Convert.ToInt16(button.Tag.ToString());//獲取當前按鈕的鍵值
            if (theKey == key)//如果與當前操作的按鈕相同
            {
                buttonList.Add(button);//添加當前操作的按鍵信息
                break;
            }
        }
        return buttonList;
    }
}
public partial class FecitButton : Button
{
    public FecitButton()
    {
        InitializeComponent();
        base.Font = new Font("宋體", 9.75F, FontStyle.Bold);
        base.Width = 26;
        base.Height = 25;
    }

    private Color Color_Brush = Color.MediumPurple;
    private Color Color_Pen = Color.Indigo;
    public static bool MouseE = false;

    private bool TChecked = false;
    [Browsable(true), Category("按鈕操作"), Description("設置按鈕是否被按下,如按下,則為true")]
    public bool Checked
    {
        get { return TChecked; }
        set
        {
            TChecked = value;
            Invalidate();
        }
    }

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        Color tem_colorb = Color_Brush;
        Color tem_colorp = Color_Pen;
        if (Checked)
        {
            Color_Brush = Color.Pink;
            Color_Pen = Color.PaleVioletRed;
        }
        else
        {
            Color_Brush = tem_colorb;
            Color_Pen = tem_colorp;
        }
        SolidBrush tem_Brush = new SolidBrush(Color_Brush);
        Pen tem_Pen = new Pen(new SolidBrush(Color_Pen), 2);
        e.Graphics.FillRectangle(tem_Brush, 0, 0, this.Width, this.Height);
        e.Graphics.DrawRectangle(tem_Pen, 1, 1, this.Width - 2, this.Height - 2);
        ProtractText(e.Graphics);
    }

    /// <summary>
    /// 鼠標移入控件的可見區(qū)域時觸發(fā)
    /// </summary>
    protected override void OnMouseEnter(EventArgs e)
    {
        Color_Brush = Color.LightSteelBlue;
        Color_Pen = Color.LightSlateGray;
        Invalidate();
        base.OnMouseEnter(e);
    }

    /// <summary>
    /// 鼠標移出控件的可見區(qū)域時觸發(fā)
    /// </summary>
    protected override void OnMouseLeave(EventArgs e)
    {
        Color_Brush = Color.MediumPurple;
        Color_Pen = Color.Indigo;
        Invalidate();
        base.OnMouseLeave(e);
    }

    private void ProtractText(Graphics g)
    {
        Graphics TitG = this.CreateGraphics();//創(chuàng)建Graphics類對象
        string TitS = base.Text;//獲取圖表標題的名稱
        SizeF TitSize = TitG.MeasureString(TitS, this.Font);//將繪制的字符串進行格式化
        float TitWidth = TitSize.Width;//獲取字符串的寬度
        float TitHeight = TitSize.Height;//獲取字符串的高度
        float TitX = 0;//標題的橫向坐標
        float TitY = 0;//標題的縱向坐標
        if (this.Height > TitHeight)
            TitY = (this.Height - TitHeight) / 2F;
        else
            TitY = 1;
        if (this.Width > TitWidth)
            TitX = (this.Width - TitWidth) / 2F;
        else
            TitX = 2;
        Rectangle rect = new Rectangle((int)Math.Floor(TitX), (int)Math.Floor(TitY), (int)Math.Ceiling(TitWidth), (int)Math.Ceiling(TitHeight));
        g.DrawString(TitS, base.Font,new SolidBrush(base.ForeColor), new PointF(TitX, TitY));

    }
}
internal static class KeyboardConstaint
{
    internal static readonly short VK_F1 = 0x70;
    internal static readonly short VK_F2 = 0x71;
    internal static readonly short VK_F3 = 0x72;
    internal static readonly short VK_F4 = 0x73;
    internal static readonly short VK_F5 = 0x74;
    internal static readonly short VK_F6 = 0x75;
    internal static readonly short VK_F7 = 0x76;
    internal static readonly short VK_F8 = 0x77;
    internal static readonly short VK_F9 = 0x78;
    internal static readonly short VK_F10 = 0x79;
    internal static readonly short VK_F11 = 0x7A;
    internal static readonly short VK_F12 = 0x7B;
    internal static readonly short VK_LEFT = 0x25;
    internal static readonly short VK_UP = 0x26;
    internal static readonly short VK_RIGHT = 0x27;
    internal static readonly short VK_DOWN = 0x28;
    internal static readonly short VK_NONE = 0x00;
    internal static readonly short VK_ESCAPE = 0x1B;
    internal static readonly short VK_EXECUTE = 0x2B;
    internal static readonly short VK_CANCEL = 0x03;
    internal static readonly short VK_RETURN = 0x0D;
    internal static readonly short VK_ACCEPT = 0x1E;
    internal static readonly short VK_BACK = 0x08;
    internal static readonly short VK_TAB = 0x09;
    internal static readonly short VK_DELETE = 0x2E;
    internal static readonly short VK_CAPITAL = 0x14;
    internal static readonly short VK_NUMLOCK = 0x90;
    internal static readonly short VK_SPACE = 0x20;
    internal static readonly short VK_DECIMAL = 0x6E;
    internal static readonly short VK_SUBTRACT = 0x6D;
    internal static readonly short VK_ADD = 0x6B;
    internal static readonly short VK_DIVIDE = 0x6F;
    internal static readonly short VK_MULTIPLY = 0x6A;
    internal static readonly short VK_INSERT = 0x2D;
    internal static readonly short VK_OEM_1 = 0xBA;  // ';:' for US
    internal static readonly short VK_OEM_PLUS = 0xBB;  // '+' any country
    internal static readonly short VK_OEM_MINUS = 0xBD;  // '-' any country
    internal static readonly short VK_OEM_2 = 0xBF;  // '/?' for US
    internal static readonly short VK_OEM_3 = 0xC0;  // '`~' for US
    internal static readonly short VK_OEM_4 = 0xDB;  //  '[{' for US
    internal static readonly short VK_OEM_5 = 0xDC;  //  '\|' for US
    internal static readonly short VK_OEM_6 = 0xDD;  //  ']}' for US
    internal static readonly short VK_OEM_7 = 0xDE;  //  ''"' for US
    internal static readonly short VK_OEM_PERIOD = 0xBE;  // '.>' any country
    internal static readonly short VK_OEM_COMMA = 0xBC;  // ',<' any country
    internal static readonly short VK_SHIFT = 0x10;
    internal static readonly short VK_CONTROL = 0x11;
    internal static readonly short VK_MENU = 0x12;
    internal static readonly short VK_LWIN = 0x5B;
    internal static readonly short VK_RWIN = 0x5C;
    internal static readonly short VK_APPS = 0x5D;
    internal static readonly short VK_LSHIFT = 0xA0;
    internal static readonly short VK_RSHIFT = 0xA1;
    internal static readonly short VK_LCONTROL = 0xA2;
    internal static readonly short VK_RCONTROL = 0xA3;
    internal static readonly short VK_LMENU = 0xA4;
    internal static readonly short VK_RMENU = 0xA5;
    internal static readonly short VK_SNAPSHOT = 0x2C;
    internal static readonly short VK_SCROLL = 0x91;
    internal static readonly short VK_PAUSE = 0x13;
    internal static readonly short VK_HOME = 0x24;
    internal static readonly short VK_NEXT = 0x22;
    internal static readonly short VK_PRIOR = 0x21;
    internal static readonly short VK_END = 0x23;
    internal static readonly short VK_NUMPAD0 = 0x60;
    internal static readonly short VK_NUMPAD1 = 0x61;
    internal static readonly short VK_NUMPAD2 = 0x62;
    internal static readonly short VK_NUMPAD3 = 0x63;
    internal static readonly short VK_NUMPAD4 = 0x64;
    internal static readonly short VK_NUMPAD5 = 0x65;
    internal static readonly short VK_NUMPAD5NOTHING = 0x0C;
    internal static readonly short VK_NUMPAD6 = 0x66;
    internal static readonly short VK_NUMPAD7 = 0x67;
    internal static readonly short VK_NUMPAD8 = 0x68;
    internal static readonly short VK_NUMPAD9 = 0x69;
    internal static readonly short KEYEVENTF_EXTENDEDKEY = 0x0001;
    internal static readonly short KEYEVENTF_KEYUP = 0x0002;
    internal static readonly int GWL_EXSTYLE = -20;
    internal static readonly int WS_DISABLED = 0X8000000;
    internal static readonly int WM_SETFOCUS = 0X0007;
}
[StructLayout(LayoutKind.Sequential)]
internal struct MOUSEINPUT
{
    public int dx;
    public int dy;
    public int mouseData;
    public int dwFlags;
    public int time;
    public IntPtr dwExtraInfo;
}

[StructLayout(LayoutKind.Sequential)]
internal struct KEYBDINPUT
{
    public short wVk;
    public short wScan;
    public int dwFlags;
    public int time;
    public IntPtr dwExtraInfo;
}

[StructLayout(LayoutKind.Explicit)]
internal struct Input
{
    [FieldOffset(0)]
    public int type;
    [FieldOffset(4)]
    public MOUSEINPUT mi;
    [FieldOffset(4)]
    public KEYBDINPUT ki;
    [FieldOffset(4)]
    public HARDWAREINPUT hi;
}

[StructLayout(LayoutKind.Sequential)]
internal struct HARDWAREINPUT
{
    public int uMsg;
    public short wParamL;
    public short wParamH;
}

internal class INPUT
{
    public const int MOUSE = 0;
    public const int KEYBOARD = 1;
    public const int HARDWARE = 2;
}

internal static class NativeMethods
{
    [DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    internal static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);

    [DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    internal static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    [DllImport("User32.dll", EntryPoint = "SendInput", CharSet = CharSet.Auto)]
    internal static extern UInt32 SendInput(UInt32 nInputs, Input[] pInputs, Int32 cbSize);

    [DllImport("Kernel32.dll", EntryPoint = "GetTickCount", CharSet = CharSet.Auto)]
    internal static extern int GetTickCount();

    [DllImport("User32.dll", EntryPoint = "GetKeyState", CharSet = CharSet.Auto)]
    internal static extern short GetKeyState(int nVirtKey);

    [DllImport("User32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
    internal static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
}

到此這篇關于C#實現(xiàn)虛擬鍵盤的實例詳解的文章就介紹到這了,更多相關C#虛擬鍵盤內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論