自己写的虚拟键盘灯小挂件

时间:2020-12-16 00:48:45


最近我的电脑经常在接上外接键盘和不接外接键盘之间来回.经常接键盘的时候喜欢开上小键盘,完事之后又忘记切换回来.而后就在网上简单搜了下相关的能在屏幕上显示键盘的三个锁定(大写.数字,滚动)状态的软件.但是竟然没有找到.......于是就自己写了个.和大家分享下

下面贴一下主要的源码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Diagnostics;
namespace ShowKeyLockMode
{

public partial class IsKeyLock : Form
{

string path = Process.GetCurrentProcess().MainModule.FileName;

const uint KEYEVENTF_EXTENDEDKEY = 0x1;
const uint KEYEVENTF_KEYUP = 0x2;
public enum VirtualKeys : byte
{
VK_NUMLOCK = 0x90, //数字锁定键
VK_SCROLL = 0x91, //滚动锁定
VK_CAPITAL = 0x14, //大小写锁定
}
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
/// <summary>
/// 大写是否锁定
/// </summary>
public bool IsCapsLocked()
{
return IsKeyLocked(Keys.CapsLock);
}
/// <summary>
/// 小键盘是否锁定
/// </summary>
/// <returns></returns>
public bool IsNumberLock()
{
return IsKeyLocked(Keys.NumLock);
}
/// <summary>
/// 滚动是否锁定
/// </summary>
/// <returns></returns>
public bool IsScrollLock()
{
return IsKeyLocked(Keys.Scroll);
}
/// <summary>
/// 定时扫描按键的锁定状态的定时器
/// </summary>
protected Timer checkKeyboardTimer;
public IsKeyLock()
{
this.TopMost = true;
InitializeComponent();
//不显示任务栏图标
this.ShowInTaskbar = false;

this.StartPosition = FormStartPosition.Manual;
int iActulaWidth = Screen.PrimaryScreen.Bounds.Width;
int iActulaHeight = Screen.PrimaryScreen.Bounds.Height;
this.Left = iActulaWidth - this.Width;
this.Top = iActulaHeight - this.Height * 2;

this.flowLayoutPanel1.MouseMove += flowLayoutPanel1_MouseMove; ;
this.flowLayoutPanel1.MouseDown += flowLayoutPanel1_MouseDown;
this.flowLayoutPanel1.MouseClick += flowLayoutPanel1_MouseClick;
checkKeyboardTimer = new Timer();
checkKeyboardTimer.Tick += CheckKeyboardTimer_Tick;
checkKeyboardTimer.Interval = 50;
checkKeyboardTimer.Start();

runWhenStart(true, "键盘锁定状态显示", path);
}

void flowLayoutPanel1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
this.contextMenuStrip1.Show(e.X + this.Left, e.Y + this.Top);
}
}

/// <summary>
/// 鼠标点击时.记录下点击时,鼠标相对于控件的位置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void flowLayoutPanel1_MouseDown(object sender, MouseEventArgs e)
{
this.heightToTop = e.Y;
this.widthToLeft = e.X;
}

int heightToTop = 0;
int widthToLeft = 0;
/// <summary>
/// 鼠标按下并移动时,移动窗体位置以维持相对距离
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void flowLayoutPanel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left || e.Button == MouseButtons.Right)
{
int x = e.X - widthToLeft;
int y = e.Y - heightToTop;
this.Left += x;
this.Top += y;
}
}

/// <summary>
/// 定时扫描键盘的锁定状态的Tick
/// </summary>
void CheckKeyboardTimer_Tick(object sender, EventArgs e)
{


btCapsLock.Image = IsCapsLocked() ? global::ShowKeyLockMode.Properties.Resources.caps_Locked : global::ShowKeyLockMode.Properties.Resources.caps_Unlock;
btNumberLock.Image = IsNumberLock() ? global::ShowKeyLockMode.Properties.Resources.number_Locked : global::ShowKeyLockMode.Properties.Resources.number_Unlock;

btScrollLock.Image = IsScrollLock() ? global::ShowKeyLockMode.Properties.Resources.Scroll_Locked : global::ShowKeyLockMode.Properties.Resources.Scroll_Unlock;
}
/// <summary>
/// 大小写锁定按钮按下的事件
/// </summary>
private void btCapsLock_Click(object sender, EventArgs e)
{
PressKey((byte)VirtualKeys.VK_CAPITAL);
}
/// <summary>
/// 滚动锁定按钮按下的事件
/// </summary>
private void btScrollLock_Click(object sender, EventArgs e)
{
PressKey((byte)VirtualKeys.VK_SCROLL);
}
/// <summary>
/// 数字键锁定按钮的按下事件
/// </summary>
private void btNumberLock_Click(object sender, EventArgs e)
{
PressKey((byte)VirtualKeys.VK_NUMLOCK);
}
/// <summary>
/// 按下一个键
/// </summary>
/// <param name="key">按键的代码</param>
protected void PressKey(byte key)
{
keybd_event(key, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
keybd_event(key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
/// <summary>
/// 设置程序开机启动
/// 或取消开机启动
/// </summary>
/// <param name="started">设置开机启动,或者取消开机启动</param>
/// <param name="exeName">注册表中程序的名字</param>
/// <param name="path">开机启动的程序路径</param>
/// <returns>开启或则停用是否成功</returns>
public static bool runWhenStart(bool started, string exeName, string path)
{
RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项
if (key == null)//如果该项不存在的话,则创建该子项
{
key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
}
if (started == true)
{
try
{
key.SetValue(exeName, path);//设置为开机启动
key.Close();
}
catch
{
return false;
}
}
else
{
try
{
key.DeleteValue(exeName);//取消开机启动
key.Close();
}
catch
{
return false;
}
}
return true;
}


private void AutoRunToolStripMenuItem_Click(object sender, EventArgs e)
{
runWhenStart(true, "键盘锁定状态显示", path);
}

private void CloseAutoRunToolStripMenuItem_Click(object sender, EventArgs e)
{
runWhenStart(false, "键盘锁定状态显示", path);
}

private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
{
new AboutBox1().ShowDialog();
}
}
}

程序安装包下载地址