Einzelnen Beitrag anzeigen

Sim_Star

Registriert seit: 18. Jan 2006
Ort: Krostitz
63 Beiträge
 
#1

RegisterHotKey Probleme

  Alt 17. Jun 2015, 13:20
Hallo, ich bastel mit gerade ein Programm welches systemweit bestimmte HotKeys aufgreift und dann weiter verarbeitet.

Die Hotkeys sind in einer ini File definiert.

Ich lese die Hotkeys aus der Inifile ein und Registriere diese alle.
Nur eben da scheint es ein Problem zu geben. Es wird immer nur der 1. Hotkey aus der Ini registriert, und die anderen alle nicht.
Dieser wird dann auch erkannt wenn man Ihn benutzt.
Wo könnte dann also mein Fehler liegen?
Hier mal mein Code:

hotkeys.ini

Code:
;Skalarki Predefined hotkeys

[HotKey.0]
; HotKey listening for
key1=CTRL
key2=F9
; yes/no yes=> new keystroke will be send to application. no=> same keystroke will be send to application
add_new_keystroke=no
; new transfered keystroke
new_keystroke=
; name off application where new keystroke has to send to
application_name=
; part of window title of application to find window handle
application_caption=
; press and release
; yes => press_event for defined keystroke will be send
press=yes
; yes => release_event for defined keystroke will be send
release=yes

[HotKey.1]
key1=F1
key2=CTRL
add_new_keystroke=no
new_keystroke=
application_name=
application_caption=
press=yes
release=yes

[HotKey.2]
key1=F2
key2=
add_new_keystroke=no
new_keystroke=
application_name=
application_caption=
press=yes
release=yes

[HotKey.3]
key1=F3
key2=
add_new_keystroke=no
new_keystroke=
application_name=
application_caption=
press=yes
release=yes

[HotKey.4]
key1=F4
key2=
add_new_keystroke=no
new_keystroke=
application_name=
application_caption=
press=yes
release=yes

[HotKey.5]
key1=F5
key2=
add_new_keystroke=no
new_keystroke=
application_name=
application_caption=
press=yes
release=yes


Delphi-Quellcode:
unit HotKeyApp;

interface

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

const
  VK_NULL = $00; // NULL
  VK_LBUTTON = $01; // Linke Maustaste
  VK_RBUTTON = $02; // Rechte Maustaste
  VK_CANCEL = $03; // Für die Verarbeitung von Control-Break
  VK_MBUTTON = $04; // Mittlere Maustaste (Dreitastenmaus)

  VK_BACK = $08; // BACKSPACE Taste
  VK_TAB = $09; // TAB Taste

  VK_CLEAR = $0C; // Löschtaste
  VK_RETURN = $0D; // RETURN Taste

  VK_SHIFT = $10; // SHIFT Taste
  VK_CONTROL = $11; // STRG Taste
  VK_MENU = $12; // ALT Taste
  VK_PAUSE = $13; // PAUSE Taste
  VK_CAPITAL = $14; // CAPS LOCK Taste

  VK_ESCAPE = $1B; // ESC Taste

  VK_SPACE = $20; // LEERTASTE
  VK_PRIOR = $21; // PAGE UP Taste
  VK_NEXT = $22; // PAGE DOWN Taste
  VK_END = $23; // ENDE Taste
  VK_HOME = $24; // POS1 Taste
  VK_LEFT = $25; // Cursortaste links
  VK_UP = $26; // Cursortaste oben
  VK_RIGHT = $27; // Cursortaste rechts
  VK_DOWN = $28; // Cursortaste unten
  VK_SELECT = $29; // SELECT Taste
  VK_PRINT = $2A; // Druck
  VK_EXECUTE = $2B; // Taste Ausführen (EXECUTE)
  VK_SNAPSHOT = $2C; // PRINT SCREEN Taste für Windows 3.0 und höher
  VK_INSERT = $2D; // EINFG Taste
  VK_DELETE = $2E; // ENTF Taste
  VK_HELP = $2F; // Hilfetaste
  VK_0 = $30; // Taste 0
  VK_1 = $31; // Taste 1
  VK_2 = $32; // Taste 2
  VK_3 = $33; // Taste 3
  VK_4 = $34; // Taste 4
  VK_5 = $35; // Taste 5
  VK_6 = $36; // Taste 6
  VK_7 = $37; // Taste 7
  VK_8 = $38; // Taste 8
  VK_9 = $39; // Taste 9

  VK_A = $41; // Taste A
  VK_B = $42; // Taste B
  VK_C = $43; // Taste C
  VK_D = $44; // Taste D
  VK_E = $45; // Taste E
  VK_F = $46; // Taste F
  VK_G = $47; // Taste G
  VK_H = $48; // Taste H
  VK_I = $49; // Taste I
  VK_J = $4A; // Taste J
  VK_K = $4B; // Taste K
  VK_L = $4C; // Taste L
  VK_M = $4D; // Taste M
  VK_N = $4E; // Taste N
  VK_O = $4F; // Taste O
  VK_P = $50; // Taste P
  VK_Q = $51; // Taste Q
  VK_R = $52; // Taste R
  VK_S = $53; // Taste S
  VK_T = $54; // Taste T
  VK_U = $55; // Taste U
  VK_V = $56; // Taste V
  VK_W = $57; // Taste W
  VK_X = $58; // Taste X
  VK_Y = $59; // Taste Y
  VK_Z = $5A; // Taste Z
  VK_LWIN = $5B; // Linke Windowstaste
  VK_RWIN = $5C; // Rechte Windowstaste
  VK_APPS = $5D; // Application-Taste

  VK_NUMPAD0 = $60; // Numerisches Tastenfeld, Taste 0
  VK_NUMPAD1 = $61; // Numerisches Tastenfeld, Taste 1
  VK_NUMPAD2 = $62; // Numerisches Tastenfeld, Taste 2
  VK_NUMPAD3 = $63; // Numerisches Tastenfeld, Taste 3
  VK_NUMPAD4 = $64; // Numerisches Tastenfeld, Taste 4
  VK_NUMPAD5 = $65; // Numerisches Tastenfeld, Taste 5
  VK_NUMPAD6 = $66; // Numerisches Tastenfeld, Taste 6
  VK_NUMPAD7 = $67; // Numerisches Tastenfeld, Taste 7
  VK_NUMPAD8 = $68; // Numerisches Tastenfeld, Taste 8
  VK_NUMPAD9 = $69; // Numerisches Tastenfeld, Taste 9
  VK_MULTIPLY = $6A; // Multiplikationstaste
  VK_ADD = $6B; // Additionstaste
  VK_SEPARATOR = $6C; // Trenntaste
  VK_SUBTRACT = $6D; // Subtraktionstaste
  VK_DECIMAL = $6E; // Dezimaltrenntaste
  VK_DIVIDE = $6F; // Divisionstaste
  VK_F1 = $70; // F1 Taste
  VK_F2 = $71; // F2 Taste
  VK_F3 = $72; // F3 Taste
  VK_F4 = $73; // F4 Taste
  VK_F5 = $74; // F5 Taste
  VK_F6 = $75; // F6 Taste
  VK_F7 = $76; // F7 Taste
  VK_F8 = $77; // F8 Taste
  VK_F9 = $78; // F9 Taste
  VK_F10 = $79; // F10 Taste
  VK_F11 = $7A; // F11 Taste
  VK_F12 = $7B; // F12 Taste
  VK_F13 = $7C; // F13 Taste
  VK_F14 = $7D; // F14 Taste
  VK_F15 = $7E; // F15 Taste
  VK_F16 = $7F; // F16 Taste
  VK_F17 = $80; // F17 Taste
  VK_F18 = $81; // F18 Taste
  VK_F19 = $82; // F19 Taste
  VK_F20 = $83; // F20 Taste
  VK_F21 = $84; // F21 Taste
  VK_F22 = $85; // F22 Taste
  VK_F23 = $86; // F23 Taste
  VK_F24 = $87; // F24 Taste

  VK_NUMLOCK = $90; // NUM LOCK Taste
  VK_SCROLL = $91; // SCROLL LOCK Taste
  VK_LSHIFT = $A0; // Linke Shift-Taste
  VK_RSHIFT = $A1; // Rechte Shift-Taste
  VK_LCONTROL = $A2; // Linke Strg-Taste
  VK_RCONTORL = $A3; // Rechte Strg-Taste
  VK_LMENU = $A4; // Linke Menü-Taste
  VK_RMENU = $A5; // Rechte Menü-Taste
  VK_SEMICOLON = $BA; // Taste ;
  VK_EQUAL = $BB; // Taste =
  VK_COMMA = $BC; // Taste ,
  VK_MINUS = $BD; // Taste -
  VK_PERIOD = $BE; // Taste .
  VK_SLASH = $BF; // Taste \
  VK_BACKQUOTE = $C0; // Taste `
  VK_LEFTBRACKET = $DB; // Taste <
  VK_BACKSHLASH = $DC; // Taste \
  VK_RIGHTBRACKET = $DD; // Taste >
  VK_QUOTE = $DE; // Taste ´

  VK_PROCESSKEY = $E5; // Process-Taste
  VK_ATTN = $F6; // Attn-Taste
  VK_CRSEL = $F7; // Clear-Selected-Taste
  VK_EXSEL = $F8; // EXSEL-Taste
  VK_EREOF = $F9; // EREOF-Taste
  VK_PLAY = $FA; // Play-Taste
  VK_ZOOM = $FB; // Zoom-Taste
  VK_NONAME = $FC; // Noname-Taste
  VK_PA1 = $FD; // PA1-Taste
  VK_OEM_CLEAR = $FE; // OEM-Clear

type THotkey = record
  HotKeyName : string;
  Key1 : cardinal;
  Key2 : cardinal;
  HotKey : integer;
  add_keystroke : boolean;
  new_keystroke : integer;
  application_name : string;
  application_caption : string;
  press : boolean;
  release : boolean;
  end;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private-Deklarationen }
    procedure WMHotKey(var Msg: TWMHotKey); message WM_HOTKEY;
  public
    { Public-Deklarationen }
    HotKeyList : array of THotkey;
    HotKeyListSize : cardinal;

    HotKeyIni : TIniFile;
    HotKey1, HotKey2, HotKey3: integer;

  end;


var
  Form1: TForm1;

implementation

{$R *.dfm}

// String in Virtuellen Key umwandeln, siehe KonstantenListe überhaltb
function StrToVK (aString : string): cardinal;
begin

  if (aString = '') then result := VK_NULL;

  // F1 bis F12
  if (aString = 'F1') then result := VK_F1;
  if (aString = 'F2') then result := VK_F2;
  if (aString = 'F3') then result := VK_F3;
  if (aString = 'F4') then result := VK_F4;
  if (aString = 'F5') then result := VK_F5;
  if (aString = 'F6') then result := VK_F6;
  if (aString = 'F7') then result := VK_F7;
  if (aString = 'F8') then result := VK_F8;
  if (aString = 'F9') then result := VK_F9;
  if (aString = 'F10') then result := VK_F10;
  if (aString = 'F11') then result := VK_F11;
  if (aString = 'F12') then result := VK_F12;

  // CTRL, ALT, SHIFT
  if (aString = 'CTRL') then result := MOD_CONTROL;
  if (aString = 'ALT') then result := VK_MENU;
  if (aString = 'LSHIFT') then result := VK_LSHIFT;
  if (aString = 'RSHIFT') then result := VK_RSHIFT;

  // A - Z

  // 0 -9

end;


procedure SetHotKey(aKey1, aKey2 : cardinal; aNumber : integer);
begin
  Form1.Memo1.Lines.Add(pchar('HotKey.' + IntToStr(aNumber)) + ' ---> ' + Form1.HotKeyList[aNumber].HotKeyName);


  Form1.HotKeyList[aNumber].HotKey := GlobalAddAtom(pchar('HotKey.' + IntToStr(aNumber)));
  RegisterHotKey(Form1.Handle, form1.HotKeyList[aNumber].HotKey ,aKey1, aKey2);
end;


procedure TForm1.WMHotKey(var Msg: TWMHotKey);
var i : integer;
begin
  //Memo1.Lines.Add('Msg.Hotkey='+ inttostr(msg.HotKey));

  if (HotKeyListSize > 0) then
    for i := 0 to HotKeyListSize - 1 do
      if (HotKeyList[i].HotKey = Msg.HotKey) then
        Memo1.lines.add('Hotkey.' + inttostr(i) + ' ---> wurde gedrückt!');


  //Form1.Caption := 'Msg.HotKey= ' +inttostr(Msg.HotKey) + 'HotKeyList[0].HotKey = ' + inttostr(HotKeyList[0].HotKey);

  //if Msg.HotKey = HotKeyList[0].HotKey then showmessage('juhu ----- ' + HotKeyList[0].HotKeyName);

end;

procedure TForm1.FormCreate(Sender: TObject);
var i, u : integer;
    key1, key2 : string;
begin
  // HotKeyIni einlesen
  HotKeyIni := TInifile.Create(ExtractFilePath(ParamStr(0)) + 'hotkeys.ini');

  for i := 0 to 255 do
    if not HotKeyIni.SectionExists('HotKey.' +IntToStr(i)) then
      break;

  // Größe des HotKeyArrays festlegen
  if (i = 0) then
    //Keine Hotkeys definiert
    HotKeyListSize := 0
  else
    begin
    HotKeyListSize := i;
    setlength(HotKeyList, i);
    end;

  // HotKeyArray befüllen
  if (HotKeyListSize > 0) then
    for i := 0 to HotKeyListSize - 1 do
      begin
      HotKeyList[i].HotKeyName := HotKeyIni.ReadString('HotKey.' + IntToStr(i),'key1','') + ' + ' +HotKeyIni.ReadString('HotKey.' + IntToStr(i),'key2','');
      HotKeyList[i].Key1 := StrToVK(HotKeyIni.ReadString('HotKey.' + IntToStr(i),'key1',''));
      HotKeyList[i].Key2 := StrToVK(HotKeyIni.ReadString('HotKey.' + IntToStr(i),'key2',''));

      setHotKey(HotKeyList[i].Key1, HotKeyList[i].Key2, i);

      Memo1.Lines.Add(inttostr(HotKeyList[i].HotKey));
      end;

end;

procedure TForm1.FormDestroy(Sender: TObject);
var i : integer;
begin
  if (HotKeyListSize > 0) then
    for i := 0 to HotKeyListSize - 1 do
      begin
      UnRegisterHotKey(Handle, HotKeyList[i].HotKey);
      GlobalDeleteAtom(HotKeyList[i].HotKey);
      end;
end;

end.

Vielen Dank schonmal im Vorraus
  Mit Zitat antworten Zitat