mmmm
No funciona. ¿En qué unidad están AllocateHWnd y DeallocateHWnd?
Utilizo Delphi 3 standar.
El código lo necesitaba para un componente, el cual analiza el cambio de estado de CapsLock, NumLock y ScrollLock. Para ello necesitaba un HotKey, y por eso necesitaba el Handle de la ventana. Este es el código (por si a alguien se le ocurre cómo solucionar el problema):
Código Delphi
[-]
unit KeyState;
interface
uses
Windows, Messages, Classes;
type
TKeyState = class(TComponent)
private
aCaps,
aNum,
aScroll: ATOM;
Active,
FCapsLock,
FNumLock,
FScrollLock: Boolean;
FHotKeyName: String;
FOnChange: TNotifyEvent;
InternalWindow: HWnd;
Procedure WndProc(var Msg: TMessage); virtual;
public
Constructor Create(AOwner: TComponent); override;
Destructor Destroy; override;
Procedure Start;
Procedure Stop;
Property CapsLock: Boolean read FCapsLock;
Property NumLock: Boolean read FNumLock;
Property ScrollLock: Boolean read FScrollLock;
Property HotKeyName: String read FHotKeyName write FHotKeyName;
published
Property OnChange: TNotifyEvent read FOnChange write FOnChange;
end;
procedure Register;
implementation
Procedure TKeyState.WndProc(var Msg: TMessage);
var
K: TKeyBoardState;
begin
If Msg.Msg=WM_HOTKEY then
with TWMHotKey(Msg) do
begin
GetKeyBoardState(K);
If HotKey=aCaps then FCapsLock:=not FCapsLock;
If HotKey=aNum then FNumLock:=not FNumLock;
If HotKey=aScroll then FScrollLock:=not FScrollLock;
K[VK_CAPITAL]:=Integer(FCapsLock);
K[VK_NUMLOCK]:=Integer(FNumLock);
K[VK_SCROLL]:=Integer(FScrollLock);
SetKeyBoardState(K);
If Assigned(FOnChange) then FOnChange(Self);
end;
end;
Constructor TKeyState.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Active:=False;
FCapsLock:=GetKeyState(VK_CAPITAL)=1;
FNumLock:=GetKeyState(VK_NUMLOCK)=1;
FScrollLock:=GetKeyState(VK_SCROLL)=1;
end;
Destructor TKeyState.Destroy;
begin
If Active then Stop;
inherited Destroy;
end;
Procedure TKeyState.Start;
begin
If FHotKeyName='' then Exit;
InternalWindow:=AllocateHWnd(WndProc);
aCaps:=GlobalAddAtom(PChar(FHotKeyName+'C'));
aNum:=GlobalAddAtom(PChar(FHotKeyName+'N'));
aScroll:=GlobalAddAtom(PChar(FHotKeyName+'S'));
RegisterHotKey(InternalWindow, aCaps, 0, VK_CAPITAL);
RegisterHotKey(InternalWindow, aNum, 0, VK_NUMLOCK);
RegisterHotKey(InternalWindow, aScroll, 0, VK_SCROLL);
Active:=True;
end;
Procedure TKeyState.Stop;
begin
If not Active then Exit;
UnRegisterHotKey(InternalWindow, aCaps);
UnRegisterHotKey(InternalWindow, aNum);
UnRegisterHotKey(InternalWindow, aScroll);
GlobalDeleteAtom(aCaps);
GlobalDeleteAtom(aNum);
GlobalDeleteAtom(aScroll);
DeallocateHWnd(InternalWindow);
Active:=False;
end;
procedure Register;
begin
RegisterComponents('LunatikO', [TKeyState]);
end;
end.