如何检测大小写键(CapsLock)的状态?

MaMaHuHu 2003-02-18 10:05:02
就是看看那个灯是不是亮?有代码最好.
...全文
327 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
MaMaHuHu 2003-02-19
  • 打赏
  • 举报
回复
谢谢版主.
TheAres 2003-02-18
  • 打赏
  • 举报
回复
调用API GetKeyState

Declare this class first:




[

ComVisibleAttribute(false),

SuppressUnmanagedCodeSecurityAttribute()

]

internal class NativeMethods

{

[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true, CallingConvention=CallingConvention.Winapi)]

public static extern short GetKeyState(int keyCode);



public static int HIWORD(int n)

{

return ((n >> 16) & 0xffff/*=~0x0000*/);

}



public static int LOWORD(int n)

{

return (n & 0xffff/*=~0x0000*/);

}

}





Then when you want to check if Caps is down or ON, call:




short state = NativeMethods.GetKeyState(0x14 /*VK_CAPTIAL*/);



bool capsKeyDown = NativeMethods.HIWORD(state);

bool capsKeyON = NativeMethods.LOWORD(state);

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧