• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C#控制键盘按键(大小写按键等)

OC/C/C++ 水墨上仙 1410次浏览

C#控制键盘按键(大小写按键等)
来源:http://blog.csdn.net/limlimlim/article/details/8251498

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace sn设置键盘大小写
{
    public partial class Form1 : Form
    {
        const uint KEYEVENTF_EXTENDEDKEY = 0x1;
        const uint KEYEVENTF_KEYUP = 0x2;
        [DllImport("user32.dll")]
        static extern short GetKeyState(int nVirtKey);
        [DllImport("user32.dll")]
        static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
        public enum VirtualKeys : byte
        {
            VK_NUMLOCK = 0x90, //数字锁定键
            VK_SCROLL = 0x91,  //滚动锁定
            VK_CAPITAL = 0x14, //大小写锁定
            VK_A = 62
        }
        public Form1()
        {
            InitializeComponent();
        }
        public static bool GetState(VirtualKeys Key)
        {
            return (GetKeyState((int)Key)==1);
        }
        public static void SetState(VirtualKeys Key, bool State)
        {
            if (State != GetState(Key))
            {
                keybd_event((byte)Key, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
                keybd_event((byte)Key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
            }
        }
        //开启键盘大写
        private void btnOpenCAPITAL_Click(object sender, EventArgs e)
        {
            SetState(VirtualKeys.VK_CAPITAL, true);
        }
        //关闭键盘大写
        private void btnCloseCAPITAL_Click(object sender, EventArgs e)
        {
            SetState(VirtualKeys.VK_CAPITAL, false);
        }
        //开启键盘滚动锁定
        private void btnOpenScroll_Click(object sender, EventArgs e)
        {
            SetState(VirtualKeys.VK_SCROLL, true);
        }
        //关闭键盘滚动锁定
        private void btnCloseScroll_Click(object sender, EventArgs e)
        {
            SetState(VirtualKeys.VK_SCROLL, false);
        }
        //开启键盘数字锁定键
        private void btnOpenNum_Click(object sender, EventArgs e)
        {
            SetState(VirtualKeys.VK_NUMLOCK, true);
        }
        //关闭键盘数字锁定键
        private void btnCloseNum_Click(object sender, EventArgs e)
        {
            SetState(VirtualKeys.VK_NUMLOCK, false);
        }
    }
}


喜欢 (0)
加载中……