如何控制键盘指示灯?(50分)

  • 主题发起人 主题发起人 eastweast
  • 开始时间 开始时间
E

eastweast

Unregistered / Unconfirmed
GUEST, unregistred user!
如何编程实现控制NumLock,CapsLock的亮和灭?
 
NumLock:
keybd_event(VK_NUMLOCK, $45, KEYEVENTF_EXTENDEDKEY or 0, 0);
keybd_event(VK_NUMLOCK, $45, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);
CapsLock:
keybd_event(20, $45, KEYEVENTF_EXTENDEDKEY or 0, 0);
keybd_event(20, $45, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);
 
用模拟按键的方式:
PostMessage(From1.Handle,WM_KEYDOWN,VK_NUMLOCK,0);
 
上面的方法可能也可以,但不正宗!
 
to:zw84611 你的方法可以,但是只能改变当前状态,如果我想判断当前是亮还是灭,要
怎么样?
To:yczjs 你的方法在win2000下没反应,98下没试过?
 
可以这样:
procedure TForm1.Button2Click(Sender: TObject);
begin

if GetKeyState(VK_NUMLOCK)=0 then Button2.Caption := 'NUMLOCK ON'
else Button2.Caption := 'NUMLOCK OFF';
keybd_event(VK_NUMLOCK, $45, KEYEVENTF_EXTENDEDKEY or 0, 0);
keybd_event(VK_NUMLOCK, $45, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);

end;

不过我想jsxjd有更好的注意。
 
GetKeyboardState
SetKeyboardState
 
to jsxjd:
xi xi,大虾,这回你好象犯了一个小错误。MSDN里是这么写的:

Remarks

Because the SetKeyboardState function alters the input state of the calling
thread and not the global input state of the system, an application cannot use
SetKeyboardState to set the num lock, caps lock, or scroll lock indicator
lights on the keyboard.
 
我从新闻组里找了一个小函数,方便调用:

procedure SetLED(Key: Byte; MakeOn: Boolean);
var
KS: TKeyboardState;
OnOrOff: Boolean;
begin
GetKeyboardState(KS);
OnOrOff:= KS[Key] <> 0;

// Wenn Status vom gewünschten abweicht
if (OnOrOff xor MakeOn) then
begin
// Je nach Plattform / Key unterschiedliche Strategien
if (Win32Platform = VER_PLATFORM_WIN32_NT)
or (Key <> VK_NUMLOCK) then
begin
// Tastendruck simulieren
keybd_event(Key, $45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(Key, $45, KEYEVENTF_EXTENDEDKEY or
KEYEVENTF_KEYUP, 0);
end
else
begin
// Gewünschten Status per Setkeyboardstate setzen
KS[Key]:= Ord(MakeOn);
SetKeyboardState(KS);
end;
end;
end;

开:SetLED(VK_NUMLOCK,true);
关:SetLED(VK_NUMLOCK,false);
 
对,我太主观性
看来在 NT 下还只能用keybd_event.

因为,在 VFP 有专门的函数,现在看来它也不是系统API。
而且 keybd_event 我是在迫不得已的情况下才用,主要是有过不成功的教训。

HOWTO: Toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK Keys
Last reviewed: December 5, 1997
Article ID: Q127190
3.50 3.51 | 4.00 WINDOWS NT | WINDOWS kbui kbcode
The information in this article applies to:

Microsoft Win32 Application Programming Interface (API) included with:


- Microsoft Windows NT versions 3.5 and 3.51
- Microsoft Windows 95 version 4.0




SUMMARY
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 reference words: 3.50 4.00 95
KBCategory: kbui kbcode
KBSubcategory: UsrInp
Keywords : UsrInp kbcode kbui
Version : 3.50 3.51 | 4.00
Platform : NT WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.





HOWTO: Toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK Keys
Last reviewed: December 5, 1997
Article ID: Q177674
The information in this article applies to:
Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 5.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 both Windows 95 and Windows NT.



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 and Windows NT require different methods for toggling these
keys.)


- Under Windows 95, use the SetKeyboardState API function to set the state
of the key. Under Windows NT, 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
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
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
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:


ARTICLE-ID: Q127190
TITLE : HOWTO: Toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK Keys


 
keybd_event何时会失效?是在win9x下吗?还请大虾指教。
 
不要动不动就拷一堆msdn出来吓人吧
给个id号大家自己去看看就好了
而且出来的都是c和vb, 又难看,又占地方
如果你加多点注解还差不多
 
多人接受答案了。
 
后退
顶部