关于截获按键的消息 (50分)

  • 主题发起人 主题发起人 Lloach
  • 开始时间 开始时间
L

Lloach

Unregistered / Unconfirmed
GUEST, unregistred user!
我需要有条件的使Enter按键事件有效,因此需截获该按键消息,根据情况使其有效!
不知如何处理?
另外,想在Richedit中使光标自动移动几个字符,又该如何处理!
先谢谢各位大虾!
 
1。用Dll Hook
2。在研究:)
 
有点相似,希望对你有所帮助
用程序中跟踪MOUSE的坐标


  为什么选这个话题?因为跟踪MOUSE坐标很常见,容易又特别不容易,非常说明WINDOWS95下编程的特点。

  { 如果您看不懂,请买DELPHI 2 UNLEASHED RMB133,当然他没这个程序,但有一些写WIN HOOK必须具备的知识。本程序得到AIMING大虾的大力协助,事实上我的程序是在他的基础上改写的,他的是从DELPHI HELP中改写出来的。调试程序花了我两个礼拜,最好你能花同样的时间,那么你就会收获很多! }

  第一步,建一DLL,DELPHI中NEW-》DLL SAVE AS GETKEY

library getKey;

uses
SysUtils,
Windows,
HookMain in 'hookmain.pas';

exports
OpenGetKeyHook,
CloseGetKeyHook,
GetPublicP;

begin
NextHook := 0;
procSaveExit := ExitProc;
DLLproc := @DLLMain;
ExitProc := @HookExit;
DLLMain(DLL_PROCESS_ATTACH);
end.

第二步,建一UNIT ,HOOK MAIN。关键在于CreateFileMapping 和 消息 WM_NCMouseMove, WM_MOUSEMOVE:

unit HookMain;

interface
uses Windows, Messages, Dialogs, SysUtils;

//type DataBuf = Array [1..2] of DWORD;
type mydata=record
data1:array [1..2] of DWORD;
data2:TMOUSEHOOKSTRUCT;
end;
var hObject : Thandle;
pMem : Pointer;
NextHook: Hhook;
procSaveExit: Pointer;

function HookHandler(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; export;
function OpenGetKeyHook(sender : HWND;MessageID : WORD) : BOOL; export;
function CloseGetKeyHook: BOOL; export;
function GetPublicP : Pointer;stdcall; export;
Procedure DLLMain(dwReason:Dword); far;
procedure HookExit; far;

implementation

Procedure UnMapMem;
begin
if Assigned(pMem) then
begin
UnMapViewOfFile(pMem);
pMem := Nil
end;
end;

Procedure MapMem;
begin
hObject := CreateFileMapping($FFFFFFFF,Nil,Page_ReadWrite,0,$FFFF,pChar('_IOBuffer'));
if hObject = 0 then Raise Exception.Create('创建公用数据的Buffer不成功!');
pMem := MapViewOfFile(hObject,FILE_MAP_WRITE,0,0,SizeOf(mydata));
// 1 or SizeOf(DataBuf) ????
// 创建SizeOf(DataBuf)的数据区
if not Assigned(pMem) then
begin
UnMapMem;
Raise Exception.Create('创建公用数据的映射关系不成功!');
end;
end;
Procedure DLLMain(dwReason:Dword); far;
begin
Case dwReason of
DLL_PROCESS_ATTACH :
begin
pMem := nil;
hObject := 0;
MapMem; //以下的公有数据,如tHWND,tMessageID将直接使用本Buf.
End;
DLL_PROCESS_DETACH : UnMapMem;
DLL_THREAD_ATTACH,
DLL_THREAD_DETACH :; //缺省
end;
end;

procedure HookExit; far;
begin
CloseGetKeyHook;
ExitProc := procSaveExit;
end;

function GetPublicP : Pointer;export;
begin //这里引出了公用数据区的指针,你可以在你的应用程序中自由操作它。但建议去掉此接口。
Result := pMem;
end;

function HookHandler(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; export;
begin
Result := 0;
If iCode < 0
Then Result := CallNextHookEx(NextHook, iCode, wParam, lParam);


// This is probably closer to what you would want to do...
Case wparam of
WM_LBUTTONDOWN:
begin
end;
WM_LBUTTONUP:
begin
end;
WM_LBUTTONDBLCLK:
begin
end;
WM_RBUTTONDOWN:
begin
messagebeep(1);
end;
WM_RBUTTONUP:
begin
end;
WM_RBUTTONDBLCLK:
begin
end;
WM_MBUTTONDOWN:
begin
end;
WM_MBUTTONUP:
begin
end;
WM_MBUTTONDBLCLK:
begin
end;
WM_NCMouseMove, WM_MOUSEMOVE:
begin
mydata(pmem^).data2:=pMOUSEHOOKSTRUCT(lparam)^;
// messagebeep(1);
//SendMessage(DataBuf(pMem^)[1],DataBuf(pMem^)[2],wParam,lParam );
SendMessage(mydata(pMem^).data1[1],mydata(pMem^).data1[2],wParam,integer(@(mydata(pmem^).data2)) );
end;
end; //发送消息
end;

function OpenGetKeyHook(sender : HWND;MessageID : WORD) : BOOL; export;
begin
Result := False;
if NextHook <> 0 then Exit; //已经安装了本钩子
// DataBuf(pMem^)[1] := Sender; //填数据区
// DataBuf(pMem^)[2] := MessageID; //填数据区
mydata(pmem^).data1[1]:=sender;
mydata(pmem^).data1[2]:=messageid;

NextHook := SetWindowsHookEx(WH_mouse, HookHandler, Hinstance, 0);
Result := NextHook <> 0;
end;

function CloseGetKeyHook: BOOL; export;
begin
if NextHook <> 0 then
begin
UnhookWindowshookEx(NextHook); //把钩子链链接到下一个钩子处理上.
NextHook := 0;
end;
Result := NextHook = 0;
end;

end.



第三步,测试DLL,建一PROJECT。关键在于override WndProc

unit Unit1;

interface

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

type
TForm1 = class(Tform)
uncapture: Tbutton;
capture: Tbutton;
Exit: Tbutton;
Panel1: Tpanel;
show: Tlabel;

Label1: Tlabel;
counter: Tlabel;
procedure ExitClick(Sender: Tobject);
procedure uncaptureClick(Sender: Tobject);
procedure captureClick(Sender: Tobject);
private
{ Private declarations }
public
{ Public declarations }
procedure WndProc(var Message: Tmessage); override;
end;

var
Form1: TForm1;
var num : integer;
const MessageID = WM_User + 100;
implementation

{$R *.DFM}
function OpenGetKeyHook(sender : HWND;MessageID : WORD) : BOOL; external 'GetKey.DLL';
function CloseGetKeyHook: BOOL; external 'GetKey.DLL';

procedure TForm1.ExitClick(Sender: Tobject);
begin
close;
end;

procedure TForm1.uncaptureClick(Sender: Tobject);
begin
if CloseGetKeyHook then //ShowMessage('结束记录...');
show.caption:='结束记录...';
end;

procedure TForm1.captureClick(Sender: Tobject);
begin
// if OpenGetKeyHook(self.Handle,MessageID) then ShowMessage('开始记录...');

if OpenGetKeyHook(Form1.Handle,MessageID) then
//ShowMessage('开始记录...');
show.caption:='开始记录...';
num := 0;


end;

procedure TForm1.WndProc(var Message: Tmessage);
var x,y:integer;
begin
if Message.Msg = MessageID then
begin
// Panel1.Caption := IntToStr(Num);
x:=PMouseHookStruct( message.lparam)^.pt.x ;
y:=PMouseHookStruct( message.lparam)^.pt.y ;

panel1.caption:='x='+inttostr(x)+' y='+inttostr(y);
inc(Num);
counter.Caption := IntToStr(Num);
end
else Inherited;
end;

end.
 
上面第一部份的DLL中的
exports
OpenGetKeyHook,
CloseGetKeyHook,
GetPublicP;
中的几个过程或函数是不是没有声明过,反正我照着做了,却不能编译通过。我用的是
DELPHL5.0;
请指教!
 
to comfan2001:
不会这么复杂吧!
我想能不能这样!先截获按键的windows消息,判断是否Enter键,而后作相应处理!
如果该Enter键无效,则将原来的消息换成没按键的空消息!
对于光标移动,能否相应的发几个按SPACE键的消息,使光标移动呢?
这些就是我提问的目的——Windows消息!不知能否实现!
 
没那么严重吧
 
是不是可以这样:
1. 截获Enter消息
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
; Shift: TShiftState);
begin
; if Key = VK_RETURN then
; begin
; ; .......
; end;
end;

2.移动光标
; richedit1.SetFocus;
; richedit1.SelStart := richedit1.SelStart + 3; //移动三个字符
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部