怎样获知Ctrl键的状态(是否处于按下状态)(100分)

  • 主题发起人 JackyKen
  • 开始时间
J

JackyKen

Unregistered / Unconfirmed
GUEST, unregistred user!
我想知道有些什么方法能知道Ctrl键当前是否处于按下状态,谢谢!
 
keyhook!等着,马上给你代码。
 
GetKeyState 函数<br>
 
看看GetKeyState的帮助。
 
好吧,我正在等,如果行就马上给分
 
好吧,先试试GetKeyState,呵呵
 
这个要全局的吧,GetKeyState 好像不是全局的。<br>dll 部分:<br>unit HKProc;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages;<br><br>var<br>&nbsp; hNextHookProc: HHook;<br>&nbsp; procSaveExit: Pointer;<br><br><br>function KeyboardHookHandler(iCode: Integer;<br>&nbsp; wParam: WPARAM;<br>&nbsp; lParam: LPARAM): LRESULT; stdcall; export;<br>function EnableHotKeyHook: BOOL; export;<br>function DisableHotKeyHook: BOOL; export;<br>procedure HotKeyHookExit; far;<br><br>implementation<br><br>function KeyboardHookHandler(iCode: Integer;<br>&nbsp; wParam: WPARAM;<br>&nbsp; lParam: LPARAM): LRESULT; stdcall; export;<br>const<br>&nbsp; _KeyPressMask = $80000000;<br>begin<br>&nbsp; Result := 0;<br>&nbsp; If iCode &lt; 0 Then<br>&nbsp; begin<br>&nbsp; &nbsp; Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br>&nbsp; // 盎代 Ctrl + B 舱?龄<br>&nbsp; if ((lParam and _KeyPressMask) = 0) and<br>&nbsp; &nbsp; (GetKeyState(vk_Control) &lt; 0) then<br>&nbsp; begin<br>&nbsp; &nbsp; MessageBox('Ctrl Is Down');<br>&nbsp; end;<br>end;<br><br><br>function EnableHotKeyHook: BOOL; export;<br>begin<br>&nbsp; Result := False;<br>&nbsp; if hNextHookProc &lt;&gt; 0 then Exit;<br>&nbsp; // 本? WH_KEYBOARD 硂?? HOOK, ??, 肚??ゲ斗玂痙?<br>&nbsp; // ㄓ, ?眔 HOOK ㊣?渺挡耞奔<br>&nbsp; hNextHookProc := SetWindowsHookEx(WH_KEYBOARD,<br>&nbsp; &nbsp; KeyboardHookHandler,<br>&nbsp; &nbsp; HInstance,<br>&nbsp; &nbsp; 0);<br>&nbsp; Result := hNextHookProc &lt;&gt; 0;<br>end;<br><br><br>function DisableHotKeyHook: BOOL; export;<br>begin<br>&nbsp; if hNextHookProc &lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; UnhookWindowshookEx(hNextHookProc); &nbsp;// 秆埃 Keyboard Hook<br>&nbsp; &nbsp; hNextHookProc := 0;<br>&nbsp; &nbsp; MessageBeep(0);<br>&nbsp; &nbsp; MessageBeep(0);<br>&nbsp; end;<br>&nbsp; Result := hNextHookProc = 0;<br>end;<br><br><br>procedure HotKeyHookExit;<br>begin<br>&nbsp; // ?狦а?秆埃 HOOK, ?笆?瞶秆埃?笆?<br>&nbsp; if hNextHookProc &lt;&gt; 0 then DisableHotKeyHook;<br>&nbsp; ExitProc := procSaveExit;<br>end;<br><br>end.<br>Exe 部分:<br>unit HookU;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; Memo1: TMemo;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>function EnableHotKeyHook: BOOL; external 'HKTEST.DLL';<br>function DisableHotKeyHook: BOOL; external 'HKTEST.DLL';<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; if EnableHotKeyHook then<br>&nbsp; &nbsp; ShowMessage('Keyboard Hook 币笆, ?? Ctrl + B 盢???癘ㄆセ?');<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>&nbsp; if DisableHotKeyHook then<br>&nbsp; &nbsp; ShowMessage('Keyboard Hook ?秆埃!');<br>end;<br><br>end.<br><br><br><br><br>
 
我来接分的。
 
对的,用getKeyState<br>SHORT GetKeyState(<br><br>&nbsp; &nbsp; int nVirtKey // virtual-key code<br>&nbsp; &nbsp;);
 
GetKeyState 是不是只有当前窗体 active 是 管用??
 
多人接受答案了。
 
顶部