[C#] C#控制键盘按键(大小写按键等) →→→→→进入此内容的聊天室

来自 , 2019-03-16, 写在 C#, 查看 174 次.
URL http://www.code666.cn/view/475fbefa
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Runtime.InteropServices;
  9.  
  10. namespace sn设置键盘大小写
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         const uint KEYEVENTF_EXTENDEDKEY = 0x1;
  15.         const uint KEYEVENTF_KEYUP = 0x2;
  16.  
  17.         [DllImport("user32.dll")]
  18.         static extern short GetKeyState(int nVirtKey);
  19.         [DllImport("user32.dll")]
  20.         static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
  21.  
  22.         public enum VirtualKeys : byte
  23.         {
  24.             VK_NUMLOCK = 0x90, //数字锁定键
  25.             VK_SCROLL = 0x91,  //滚动锁定
  26.             VK_CAPITAL = 0x14, //大小写锁定
  27.             VK_A = 62
  28.         }
  29.  
  30.         public Form1()
  31.         {
  32.             InitializeComponent();
  33.         }
  34.  
  35.         public static bool GetState(VirtualKeys Key)
  36.         {
  37.             return (GetKeyState((int)Key)==1);
  38.         }
  39.         public static void SetState(VirtualKeys Key, bool State)
  40.         {
  41.             if (State != GetState(Key))
  42.             {
  43.                 keybd_event((byte)Key, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
  44.                 keybd_event((byte)Key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  45.             }
  46.         }
  47.  
  48.         //开启键盘大写
  49.         private void btnOpenCAPITAL_Click(object sender, EventArgs e)
  50.         {
  51.             SetState(VirtualKeys.VK_CAPITAL, true);
  52.         }
  53.  
  54.         //关闭键盘大写
  55.         private void btnCloseCAPITAL_Click(object sender, EventArgs e)
  56.         {
  57.             SetState(VirtualKeys.VK_CAPITAL, false);
  58.         }
  59.  
  60.         //开启键盘滚动锁定
  61.         private void btnOpenScroll_Click(object sender, EventArgs e)
  62.         {
  63.             SetState(VirtualKeys.VK_SCROLL, true);
  64.         }
  65.  
  66.         //关闭键盘滚动锁定
  67.         private void btnCloseScroll_Click(object sender, EventArgs e)
  68.         {
  69.             SetState(VirtualKeys.VK_SCROLL, false);
  70.         }
  71.  
  72.         //开启键盘数字锁定键
  73.         private void btnOpenNum_Click(object sender, EventArgs e)
  74.         {
  75.             SetState(VirtualKeys.VK_NUMLOCK, true);
  76.         }
  77.  
  78.         //关闭键盘数字锁定键
  79.         private void btnCloseNum_Click(object sender, EventArgs e)
  80.         {
  81.             SetState(VirtualKeys.VK_NUMLOCK, false);
  82.         }
  83.  
  84.     }
  85. }
  86. //csharp/5815

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

这儿你可以回复上面这条便签

captcha