请问如何在一个应用程序中判断Caps Lock等的状态?(200分)

  • 主题发起人 主题发起人 Banny
  • 开始时间 开始时间
B

Banny

Unregistered / Unconfirmed
GUEST, unregistred user!
也就是说,我的程序如何才能知道键盘上的capslock等是否亮着?谢谢!
 
From Hot Dog
VK_INSERTI ; ;nsert¼ü
VK_NUMLOCK ; ;Num Lock
VK_CAPITAL ; ;Caps Lock
VK_SCROLL ; ; Scroll Lock

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, ComCtrls;

type
TForm1 = class(TForm)
StatusBar1: TStatusBar;
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Timer1Timer(Sender: TObject);
var ks:tkeyboardstate;
begin
; getkeyboardstate(ks);
; if odd(ks[VK_NUMLOCK]) then
; ; statusbar1.panels.items[0].text:='NUM'
; else
; ; statusbar1.panels.items[0].text:='';
; if odd(ks[VK_INSERT]) then
; ; statusbar1.panels.items[1].text:='INSERT'
; else
; ; statusbar1.panels.items[1].text:='';
; if odd(ks[VK_CAPITAL]) then
; ; statusbar1.panels.items[2].text:='CAPITAL'
; else
; ; statusbar1.panels.items[2].text:='' ;
; if odd(ks[VK_SCROLL]) then
; ; statusbar1.panels.items[3].text:='SCROLL'
; else
; ; statusbar1.panels.items[3].text:='';
; end;
end.
 
getkeyboardstate
 
soaringbird:
检测:
Var
; ks: TkeyBoardState;
begin
;GetKeyboardState(ks);
;if (ks[VK_NUMLOCK] = 1) then
; ;ShowMessage('Num Lock is on.')
;else
; ;ShowMessage('Num Lock is off.');
;if (ks[VK_CAPITAL] = 1) then
; ;ShowMessage('Caps Lock is on.')
;else
; ;ShowMessage('Caps Lock is off.');
end;

切换:
keybd_event( VK_NUMLOCK, $45, KEYEVENTF_EXTENDEDKEY or 0,0 );
;keybd_event( VK_NUMLOCK, $45, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);
;keybd_event( VK_CAPITAL, $45, KEYEVENTF_EXTENDEDKEY or 0, 0 );
;keybd_event( VK_CAPITAL, $45, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);
*******************************************
Question

How to control caps lock key?

Answer

A:
In Windows enviroment, you can look at the keyboard lights values, but you can't
set it, because Windows intercept your peek in the memory and blocks it (I tryed
under Windows 95, maybe under Windows 3.11 it works). However, you should be able
to look at the status.
Try to put this simple code in a function:
const
; ;SCROLLLOCK = 1;
; ;NUMLOCK ; ;= 2;
; ;CAPSLOCK ; = 4;
var
; ;Status: ;Byte;
; ;PntK: ; ;^Byte;
begin
; ; ;PntK := Ptr($40, $97); ; ; ; ;{directly point in memory}
; ; ;Status := Byte(PntK^); ; ; ; ;{read the status}
; ; ;if (NUMLOCK and Status) = NUMLOCK then ; ;{if NUM LOCK is on}
; ; ; ; ;Status := Status and (255 - NUMLOCK) ; ;{turn it off}
; ; ;else
; ; ; ; ;Status := Status or 2; ; ; ; ; ; ;{turn it on}
; ; ;Pntk^ := Status; ; ; ; ; ; ; ; ;{poke in memory (don't works)}
end;

A:

I use this procedures to turn on the caps lock if it isn't already on when
the user enters my DBloockup combo. ;This gets rid of the nasty problem
of case-sensitive indexes.
procedure TMainForm.StudentLookupEnter(Sender: TObject);
Var Level : Integer;
; ; KeyState : TKeyBoardState;
begin
; {check if caps-lock is on - if not turn it on}
; Level := GetKeyState(VK_CAPITAL);
; GetKeyboardState(KeyState);
; CapsLockStatus := KeyState;
; If Level = 0 then
; ; begin
; ; ; KeyState[VK_CAPITAL] := 1;
; ; ; setKeyboardState(KeyState);
; ; end;
end;



Question

I need my application to be able to "stuff keystrokes" into the keyboard
buffer.
My application needs to be able to do this while minimzed and the
keystrokes should effect the active Window and appear "typed".

Answer
A:
{this proc interrogates the numlock key and sets the state}
{according to the value of bOn}
procedure TIndexForm.ToggleNumLockKey(bOn: Boolean);
var
; KeyState : TKeyBoardState;
begin
; GetKeyboardState( KeyState );
; if bOn then KeyState[VK_NUMLOCK] := 1
; ; ;else KeyState[VK_NUMLOCK] := 0;
; SetKeyboardState( KeyState );
end;
 
多人接受答案了。
 
后退
顶部