如何控制键盘上面的3 个灯的亮灭 (NUM LOCK ,CAPS LOCK SRCOLL LOCK)

green221 2003-02-14 12:23:36
如何控制键盘上面的3 个灯的亮灭 (NUM LOCK ,CAPS LOCK SRCOLL LOCK)
...全文
459 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
mostneed 2003-02-14
  • 打赏
  • 举报
回复
#include "winable.h"

INPUT input[2];
memset(input, 0, 2*sizeof(INPUT));
input[0].type = input[1].type = INPUT_KEYBOARD;
input[0].ki.wVk = input[1].ki.wVk = VK_CAPITAL;
input[1].ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(2, input, sizeof(INPUT));
green221 2003-02-14
  • 打赏
  • 举报
回复
有 没有代码阿

我是个菜鸟

让我看看
yinx 2003-02-14
  • 打赏
  • 举报
回复
用sendmessage模拟 caps lock...被按下
yins 2003-02-14
  • 打赏
  • 举报
回复
好!!
masterz 2003-02-14
  • 打赏
  • 举报
回复
//彻底控制三个LOCK键

// Scroll Lock
void CMy011130D00Dlg::OnScrollBtn()
{
osd_set_leds(4);
}

// Num Lock
void CMy011130D00Dlg::OnNumBtn()
{
osd_set_leds(1);
}

// Caps Lock
void CMy011130D00Dlg::OnCapsBtn()
{
osd_set_leds(2);
}

void CMy011130D00Dlg::OnButton1()
{
int new_led = 4;
int old_led = osd_get_leds();

new_led = old_led & (~new_led);

osd_set_leds(new_led);
}

// Scroll,Num and Caps Lock
void CMy011130D00Dlg::OnSetallBtn()
{
osd_set_leds(7);
}

// no Scroll,Num and Caps Lock
void CMy011130D00Dlg::OnSet0Btn()
{
int original_leds;
original_leds = osd_get_leds();

Sleep(100);
osd_set_leds(0);
Sleep(100);

osd_set_leds(original_leds);
}

//============================================================
// osd_get_leds
//============================================================

int osd_get_leds(void)
{
BYTE key_states[256];
int result = 0;

// get the current state
GetKeyboardState(&key_states[0]);

// set the numl0ck bit
result |= (key_states[VK_NUMLOCK] & 1);
result |= (key_states[VK_CAPITAL] & 1) << 1;
result |= (key_states[VK_SCROLL] & 1) << 2;
return result;
}

//============================================================
// osd_set_leds
//============================================================

void osd_set_leds(int state)
{
static OSVERSIONINFO osinfo = { sizeof(OSVERSIONINFO) };
static int version_ready = 0;
BYTE key_states[256];
int oldstate, newstate;

// if we don't yet have a version number, get it
if (!version_ready)
{
version_ready = 1;
GetVersionEx(&osinfo);
}

// thanks to Lee Taylor for the original version of this code

// get the current state
GetKeyboardState(&key_states[0]);

// see if the numlock key matches the state
oldstate = key_states[VK_NUMLOCK] & 1;
TRACE("%d\n",oldstate);
newstate = state & 1;

// if not, simulate a key up/down
if (oldstate != newstate && osinfo.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS)
{
keybd_event(VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
keybd_event(VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
key_states[VK_NUMLOCK] = (key_states[VK_NUMLOCK] & ~1) | newstate;

// see if the caps lock key matches the state
oldstate = key_states[VK_CAPITAL] & 1;
newstate = (state >> 1) & 1;

// if not, simulate a key up/down
if (oldstate != newstate && osinfo.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS)
{
keybd_event(VK_CAPITAL, 0x3a, 0, 0);
keybd_event(VK_CAPITAL, 0x3a, KEYEVENTF_KEYUP, 0);
}
key_states[VK_CAPITAL] = (key_states[VK_CAPITAL] & ~1) | newstate;

// see if the scroll lock key matches the state
oldstate = key_states[VK_SCROLL] & 1;
newstate = (state >> 2) & 1;

// if not, simulate a key up/down
if (oldstate != newstate && osinfo.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS)
{
keybd_event(VK_SCROLL, 0x46, 0, 0);
keybd_event(VK_SCROLL, 0x46, KEYEVENTF_KEYUP, 0);
}
key_states[VK_SCROLL] = (key_states[VK_SCROLL] & ~1) | newstate;

// if we're on Win9x, use SetKeyboardState
if (osinfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
SetKeyboardState(&key_states[0]);
}
fbmsf 2003-02-14
  • 打赏
  • 举报
回复
msdn上有列子的


The documentation for SetKeyboardState() correctly says that you cannot use this API to toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK keys.

You can use keybd_event() to toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK keys under Windows NT. The same technique works for toggling CAPS LOCK and SCROLL LOCK under Windows 95, but it will not work for NUM LOCK.



MORE INFORMATION
The following sample program turns the NUM LOCK light on if it is off. The SetNumLock function defined here simulates pressing the NUM LOCK key, using keybd_event() with a virtual key of VK_NUMLOCK. It takes a boolean value that indicates whether the light should be turned off (FALSE) or on (TRUE).

The same technique can be used for the CAPS LOCK key (VK_CAPITAL) and the SCROLL LOCK key (VK_SCROLL).

Sample Code

/* Compile options needed:
*/

#include <windows.h>

void SetNumLock( BOOL bState )
{
BYTE keyState[256];

GetKeyboardState((LPBYTE)&keyState);
if( (bState && !(keyState[VK_NUMLOCK] & 1)) ||
(!bState && (keyState[VK_NUMLOCK] & 1)) )
{
// Simulate a key press
keybd_event( VK_NUMLOCK,
0x45,
KEYEVENTF_EXTENDEDKEY | 0,
0 );

// Simulate a key release
keybd_event( VK_NUMLOCK,
0x45,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
0);
}
}

void main()
{
SetNumLock( TRUE );
}

Additional query words:

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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