• 全部
  • VC综合技术
  • 互联网技术
  • MFC AppLauncher
  • .NET 技术
  • 界面
  • 进程
  • 算法
  • 硬件/系统
  • 数据库
  • VC++技术资源

各位,可由谁知道如何通过编程控制Num Lock,Caps Lock,Scroll Lock那几个灯的亮灭,请指教,小弟奉上160,如能解决,在加300分

dsli 2001-07-28 09:22:26
加精
...全文
526 点赞 收藏 16
写回复
16 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
colarcui 2002-01-29
ok
回复
jinxi_gao 2002-01-08
keybd_event()用上不同的参数就可以,察看MSDN
回复
_xiaolifeidao 2001-12-19
ME TOO
回复
ariso 2001-12-10
同意 prog_st(st)见《VB winapi程序设计》

回复
claider 2001-11-10
同意sundayboys,见《windows程序设计》
回复
chn 2001-10-30
rlgz
回复
dsli 2001-09-15
sdf
回复
yuxuguang 2001-08-04
:)
回复
prog_st 2001-07-28
嘿嘿!这下满意了吧!
回复
prog_st 2001-07-28
HOWTO: Toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK Keys

Q177674


--------------------------------------------------------------------------------
The information in this article applies to:

Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, versions 5.0, 6.0
Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 32-bit only, for Windows, version 4.0

--------------------------------------------------------------------------------


SUMMARY
This article demonstrates how to toggle the NUM LOCK, CAPS LOCK, AND SCROLL LOCK keys under Windows 95, Windows 98, Windows NT, or Windows 2000.



MORE INFORMATION
To toggle the NUM LOCK, CAPS LOCK, or SCROLL LOCK keys, you can use the following logic:


Use the GetKeyboardState function to determine the state of the key.


Determine which operating system is being used with the GetVersionEx API. (Windows 95/98 and Windows NT/2000 require different methods for toggling these keys.)


Under Windows 95 or Windows 98, use the SetKeyboardState API function to set the state of the key. Under Windows NT or Windows 2000, use the keybd_event function to simulate a key press.



This example shows how to toggle these three keys to "on" if they are "off." This sample may be easily modified to toggle them off or just to check their state.

Sample Project
Start a new Standard EXE project in Visual Basic. Form1 is created by default.


Add a CommandButton to Form1.


Add the following code to the General Declarations section of Form1:



' Declare Type for API call:
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128 ' Maintenance string for PSS usage
End Type

' API declarations:

Private Declare Function GetVersionEx Lib "kernel32" _
Alias "GetVersionExA" _
(lpVersionInformation As OSVERSIONINFO) As Long

Private Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, _
ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Declare Function GetKeyboardState Lib "user32" _
(pbKeyState As Byte) As Long

Private Declare Function SetKeyboardState Lib "user32" _
(lppbKeyState As Byte) As Long

' Constant declarations:
Const VK_NUMLOCK = &H90
Const VK_SCROLL = &H91
Const VK_CAPITAL = &H14
Const KEYEVENTF_EXTENDEDKEY = &H1
Const KEYEVENTF_KEYUP = &H2
Const VER_PLATFORM_WIN32_NT = 2
Const VER_PLATFORM_WIN32_WINDOWS = 1

Add the following code to the Click event of the CommandButton:



Private Sub Command1_Click()
Dim o As OSVERSIONINFO
Dim NumLockState As Boolean
Dim ScrollLockState As Boolean
Dim CapsLockState As Boolean

o.dwOSVersionInfoSize = Len(o)
GetVersionEx o
Dim keys(0 To 255) As Byte
GetKeyboardState keys(0)

' NumLock handling:
NumLockState = keys(VK_NUMLOCK)
If NumLockState <> True Then 'Turn numlock on
If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then '=== Win95/98

keys(VK_NUMLOCK) = 1
SetKeyboardState keys(0)
ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then '=== WinNT
'Simulate Key Press
keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
'Simulate Key Release
keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY _
Or KEYEVENTF_KEYUP, 0
End If
End If

' CapsLock handling:
CapsLockState = keys(VK_CAPITAL)
If CapsLockState <> True Then 'Turn capslock on
If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then '=== Win95/98
keys(VK_CAPITAL) = 1
SetKeyboardState keys(0)
ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then '=== WinNT
'Simulate Key Press
keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
'Simulate Key Release
keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY _
Or KEYEVENTF_KEYUP, 0
End If
End If

' ScrollLock handling:
ScrollLockState = keys(VK_SCROLL)
If ScrollLockState <> True Then 'Turn Scroll lock on
If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then '=== Win95/98
keys(VK_SCROLL) = 1
SetKeyboardState keys(0)
ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then '=== WinNT
'Simulate Key Press
keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
'Simulate Key Release
keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY _
Or KEYEVENTF_KEYUP, 0
End If
End If
End Sub

Press the F5 key to run the program. Click the CommandButton. The state of the CAPS LOCK, the NUM LOCK, and the SCROLL LOCK keys should all be "on."





REFERENCES
For additional information, please see the following articles in the Microsoft Knowledge Base:


Q127190 : HOWTO: Toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK Keys



Additional query words: numlock capslock scrolllock key state kbKeyboard

Keywords : kbSDKWin32 kbVBp kbVBp400 kbVBp500 kbVBp600 kbGrpVB kbDSupport
Issue type : kbhowto
Technology :


Last Reviewed: April 19, 2000
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.




--------------------------------------------------------------------------------
Send feedback to MSDN.Look here for MSDN Online resources.
回复
prog_st 2001-07-28
还有一个例程
#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 );
}
回复
NowCan 2001-07-28
啊!?C语言加汇编,怎么用VB实现?
回复
dycdyc123 2001-07-28
gz
回复
prog_st 2001-07-28
HOWTO: Set the CAPS LOCK, SCROLL LOCK, and NUM LOCK States

Q94606


--------------------------------------------------------------------------------
The information in this article applies to:

Microsoft Windows Software Development Kit (SDK) 3.1

--------------------------------------------------------------------------------


SUMMARY
The current state of the CAPS LOCK, SCROLL LOCK, and NUM LOCK keys can be queried and retrieved by the GetKeyboardState function. Although the SetKeyboardState function works on Windows 3.1x, it does not work correctly on Windows 95 or Windows NT. SetKeyboardState retrieves a byte array describing the state of every key. If the low order bit of a toggle key is set, the key is "on." By changing this bit, the toggle key state can be changed.



MORE INFORMATION
The following sample code demonstrates how to change the state of any key on all platforms. Simply pass the virtual key code (VK_CAPITAL, VK_NUMLOCK or VK_SCROLL) and a flag to set or reset the key state to the PostVirtualKeyEvent:

Sample Code

#include <windows.h>

#define MAKEWORD(low, high) ((WORD)(((BYTE)(low)) | \
(((WORD)((BYTE)(high))) << 8)))

void FAR PASCAL Keybd_Event(void);

void FAR PASCAL PostVirtualKey( BYTE bVirtualKey,
BOOL fKeyUp)
{
WORD wARegister, wBRegister;

// Convert the virtual-key code to the scan code
BRegister = MAKEWORD((BYTE) MapVirtualKey(bVirtualKey, 0), 0);
ARegister = MAKEWORD(bVirtualKey, (fKeyUp ? 0x80 : 0x00));
_asm mov bx, wBRegister; // BH = 0 (No prefix)
_asm mov ax, wARegister;
Keybd_Event();
}

int PASCAL WinMain( HINSTANCE hInst,
HINSTANCE hPrevInst,
LPSTR szCmdLine,
int nCmdShow)
{
// Toggle the caps lock key
PostVirtualKey(VK_CAPITAL, FALSE);
PostVirtualKey(VK_CAPITAL, TRUE);

return 0;
}

Additional query words:

Keywords : kb16bitonly kbInput kbKeyIn kbSDKPlatform kbGrpUser kbUser
Issue type : kbhowto
Technology :


Last Reviewed: June 10, 1999
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.




--------------------------------------------------------------------------------
Send feedback to MSDN.Look here for MSDN Online resources.
回复
sundayboys 2001-07-28
实际上很简单,用GetKeyboardState、SetKeyboardState就能搞定。
回复
sundayboys 2001-07-28
呵呵,我有delphi的代码能搞定,你要吗??
回复
相关推荐
发帖
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
帖子事件
创建了帖子
2001-07-28 09:22
社区公告

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