Jeg har engang gjort en Global Hot key component det er ikke helt det du skal bruge. Men det kan Hjælpe Dig.
-----------------------------kodestart
{**********************************************************************}
{* UNIT Component P j e H o t K e y (WoodShoe) *}
{*--------------------------------------------------------------------*}
{* File name : PjeHotKey.pas *}
{*--------------------------------------------------------------------*}
{* Task : Global Hot Key. *}
{*--------------------------------------------------------------------*}
{* *}
{* Author : Peter Bøje Jensen Pje.scanvaegt@mail.dk *}
{* Developed on : 29-04/2004 Ver 1.0.0 Bild 1 *}
{* Last update : -----/---- *}
{* *}
{**********************************************************************}
unit PjeHotKey;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
Var
AltA : ShortString;
type
TPjeHotKey = class(TComponent)
private
FEnabled: Boolean;
FHotStr : ShortString;
FRegistered: Boolean;
prOldWndProc: TWndMethod;
FHotkey: TSHortcut;
FOnHotKeyPressed: TNotifyEvent;
prWindow: TWinControl;
procedure RegisterKey;
procedure UnRegisterKey;
function ShortCutToText(ShortCut: TShortCut): string;
function GetSpecialName(ShortCut: TShortCut): string;
procedure ShortCutToKey(ShortCut: TShortCut; var Key: Word; var Shift: TShiftState);
{ Private declarations }
protected
{ Protected declarations }
procedure SetEnabled(const Value: Boolean);
procedure SetHotKey(const Value: TShortcut);
Procedure TextToShortCut(Const Value: Shortstring);
procedure NewWindowProc(var Message: TMessage);
procedure Loaded; override;
public
{ Public declarations }
constructor Create(aOwner: TComponent); override;
destructor Destroy; override;
property Registered: boolean read FRegistered;
property KeyStr: ShortString read FHotStr write TextToShortCut;
published
{ Published declarations }
property Enabled: Boolean read FEnabled write SetEnabled default true;
Property Key: TSHortcut read FHotkey write SetHotKey default 32833;
property OnHotKey: TNotifyEvent read FOnHotKeyPressed write FOnHotKeyPressed;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('PJE', [TPjeHotKey]);
end;
{ TPjeHotKey }
type
TMenuKeyCap = (mkcBkSp, mkcTab, mkcEsc, mkcEnter, mkcSpace, mkcPgUp,
mkcPgDn, mkcEnd, mkcHome, mkcLeft, mkcUp, mkcRight, mkcDown, mkcIns,
mkcDel, mkcShift, mkcCtrl, mkcAlt);
Const
SmkcBkSp = 'BkSp';
SmkcTab = 'Tab';
SmkcEsc = 'Esc';
SmkcEnter = 'Enter';
SmkcSpace = 'Space';
SmkcPgUp = 'PgUp';
SmkcPgDn = 'PgDn';
SmkcEnd = 'End';
SmkcHome = 'Home';
SmkcLeft = 'Left';
SmkcUp = 'Up';
SmkcRight = 'Right';
SmkcDown = 'Down';
SmkcIns = 'Ins';
SmkcDel = 'Del';
SmkcShift = 'Shift+';
SmkcCtrl = 'Ctrl+';
SmkcAlt = 'Alt+';
var
MenuKeyCaps: array[TMenuKeyCap] of string = (
SmkcBkSp, SmkcTab, SmkcEsc, SmkcEnter, SmkcSpace, SmkcPgUp,
SmkcPgDn, SmkcEnd, SmkcHome, SmkcLeft, SmkcUp, SmkcRight,
SmkcDown, SmkcIns, SmkcDel, SmkcShift, SmkcCtrl, SmkcAlt);
constructor TPjeHotKey.Create(aOwner: TComponent);
begin
inherited;
FEnabled := True;
FHotKey := 0;
FHotStr :=' ';
FRegistered := false;
if not (csDesigning in ComponentState) then
begin
prWindow := TWinControl.CreateParented(GetDesktopWindow);
prOldWndProc := prWindow.WindowProc;
prWindow.WindowProc := NewWindowProc;
end;
end;
destructor TPjeHotKey.Destroy;
begin
if not (csDesigning in ComponentState) then
begin
UnRegisterKey;
prWindow.WindowProc := prOldWndProc;
prWindow.Free;
end;
inherited;
end;
procedure TPjeHotKey.ShortCutToKey(ShortCut: TShortCut; var Key: Word; var Shift: TShiftState);
begin
Key := ShortCut and not (scShift + scCtrl + scAlt);
Shift := [];
if ShortCut and scShift <> 0 then Include(Shift, ssShift);
if ShortCut and scCtrl <> 0 then Include(Shift, ssCtrl);
if ShortCut and scAlt <> 0 then Include(Shift, ssAlt);
end;
function TPjeHotKey.GetSpecialName(ShortCut: TShortCut): string;
var
ScanCode: Integer;
KeyName: array[0..255] of Char;
begin
Result := '';
ScanCode := MapVirtualKey(WordRec(ShortCut).Lo, 0) shl 16;
if ScanCode <> 0 then
begin
GetKeyNameText(ScanCode, KeyName, SizeOf(KeyName));
GetSpecialName := KeyName;
end;
end;
function TPjeHotKey.ShortCutToText(ShortCut: TShortCut): string;
var
Name: string;
begin
case WordRec(ShortCut).Lo of
$08, $09:
Name := MenuKeyCaps[TMenuKeyCap(Ord(mkcBkSp) + WordRec(ShortCut).Lo - $08)];
$0D: Name := MenuKeyCaps[mkcEnter];
$1B: Name := MenuKeyCaps[mkcEsc];
$20..$28:
Name := MenuKeyCaps[TMenuKeyCap(Ord(mkcSpace) + WordRec(ShortCut).Lo - $20)];
$2D..$2E:
Name := MenuKeyCaps[TMenuKeyCap(Ord(mkcIns) + WordRec(ShortCut).Lo - $2D)];
$30..$39: Name := Chr(WordRec(ShortCut).Lo - $30 + Ord('0'));
$41..$5A: Name := Chr(WordRec(ShortCut).Lo - $41 + Ord('A'));
$60..$69: Name := Chr(WordRec(ShortCut).Lo - $60 + Ord('0'));
$70..$87: Name := 'F' + IntToStr(WordRec(ShortCut).Lo - $6F);
else
Name := GetSpecialName(ShortCut);
end;
if Name <> '' then
begin
Result := '';
if ShortCut and scShift <> 0 then Result := Result + MenuKeyCaps[mkcShift];
if ShortCut and scCtrl <> 0 then Result := Result + MenuKeyCaps[mkcCtrl];
if ShortCut and scAlt <> 0 then Result := Result + MenuKeyCaps[mkcAlt];
Result := Result + Name;
end
else Result := '';
end;
Procedure TPjeHotKey.TextToShortCut(Const Value: Shortstring);
function CompareFront(var Text: string; const Front: string): Boolean;
begin
Result := False;
if (Length(Text) >= Length(Front)) and
(AnsiStrLIComp(PChar(Text), PChar(Front), Length(Front)) = 0) then
begin
Result := True;
Delete(Text, 1, Length(Front));
end;
end;
var
Key: TShortCut;
Shift: TShortCut;
_Text:String;
begin
_Text:=Value;
Shift := 0;
while True do
begin
if CompareFront(_Text, MenuKeyCaps[mkcShift]) then Shift := Shift or scShift
else if CompareFront(_Text, '^') then Shift := Shift or scCtrl
else if CompareFront(_Text, MenuKeyCaps[mkcCtrl]) then Shift := Shift or scCtrl
else if CompareFront(_Text, MenuKeyCaps[mkcAlt]) then Shift := Shift or scAlt
else Break;
end;
if _Text = '' then Exit;
for Key := $08 to $255 do { Copy range from table in ShortCutToText }
if AnsiCompareText(_Text, ShortCutToText(Key)) = 0 then
begin
SetHotKey(Key or Shift);
Exit;
end;
end;
procedure TPjeHotKey.Loaded;
begin
inherited;
if (FEnabled) and (not (csDesigning in ComponentState)) then RegisterKey;
end;
procedure TPjeHotKey.NewWindowProc(var Message: TMessage);
begin
case Message.Msg of
WM_HOTKEY:
begin
if assigned(FOnHotkeyPressed) then FOnHotKeyPressed(self);
end;
end;
prOldWndProc(Message);
end;
procedure TPjeHotKey.RegisterKey;
var
lVKey: word;
lvShift: TShiftstate;
lModifier: UINT;
begin
if not FRegistered then
begin
lModifier := 0;
ShortcutToKey(FHotkey, lvKey, lvShift);
if ssShift in lvShift then lModifier := lModifier or MOD_SHIFT;
if ssAlt in lvShift then lModifier := lModifier or MOD_ALT;
if ssCtrl in lvShift then lModifier := lModifier or MOD_CONTROL;
FRegistered := RegisterHotKey(prWindow.Handle, 0,
lModifier, LOBYTE(lVKey));
end;
end;
procedure TPjeHotKey.SetEnabled(const Value: Boolean);
begin
FEnabled := Value;
if FEnabled then RegisterKey else UnregisterKey;
end;
procedure TPjeHotKey.UnRegisterKey;
begin
if FRegistered then
begin
UnregisterHotKey(prWindow.Handle, 0);
FRegistered := False;
end;
end;
procedure TPjeHotKey.SetHotKey(const Value: TShortcut);
begin
if Value <> FHotkey then
begin
FHotStr := ShortCutToText(Value);
FHotkey := Value;
if not (csDesigning in ComponentState) then
begin
UnRegisterKey;
if FEnabled then RegisterKey;
end;
end;
end;
end.
-------------kode end
MV.
Peter Jensen