AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren

RegisterHotKey Probleme

Ein Thema von Sim_Star · begonnen am 17. Jun 2015 · letzter Beitrag vom 17. Jun 2015
Antwort Antwort
Seite 1 von 2  1 2   
Sim_Star

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

RegisterHotKey Probleme

  Alt 17. Jun 2015, 12: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
Benutzerbild von Dalai
Dalai

Registriert seit: 9. Apr 2006
1.680 Beiträge
 
Delphi 5 Professional
 
#2

AW: RegisterHotKey Probleme

  Alt 17. Jun 2015, 12:38
API-Funktionen haben in der Regel einen Rückgabewert, den man auswerten kann (und sollte). Das trifft auch auf RegisterHotKey zu.

Davon abgesehen frage ich mich, warum du die Konstanten der Virtual Keys (VK_*) nochmal wiederholst, denn die sind doch bereits in irgendeiner Unit deklariert.

Und übrigens ist es keine gute Idee, eine Schleifenvariable [ADD] einer for-Schleife [/ADD] nach dem Durchlauf derselben zu irgendwas zu benutzen, denn die Variable ist nach dem Ende der Schleife undefiniert. Hier bietet sich eine while-Schleife an.

MfG Dalai

Geändert von Dalai (17. Jun 2015 um 12:47 Uhr)
  Mit Zitat antworten Zitat
Photoner

Registriert seit: 6. Dez 2012
Ort: Nürnberg
103 Beiträge
 
Delphi 10.1 Berlin Starter
 
#3

AW: RegisterHotKey Probleme

  Alt 17. Jun 2015, 12:41
Einen kleinen Fehler habe ich schon mal gefunden:
Delphi-Quellcode:
    HotKeyListSize := i;
    setlength(HotKeyList, i);
Ok, stimmte doch
Chris

Geändert von Photoner (17. Jun 2015 um 12:44 Uhr) Grund: ich lag falsch
  Mit Zitat antworten Zitat
Sim_Star

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

AW: RegisterHotKey Probleme

  Alt 17. Jun 2015, 12:52
Vielen Dank soweit,

ich habe jetzt mal das Ergebnis von Register Hotkey abgefragt und es wird tatsächlich nur der erste Hotkey erzeugt. Wie kann ich der Ursache hier weiter auf den Grund gehen. Stehe hier erstmal auf dem Schlauch.
  Mit Zitat antworten Zitat
Benutzerbild von Dalai
Dalai

Registriert seit: 9. Apr 2006
1.680 Beiträge
 
Delphi 5 Professional
 
#5

AW: RegisterHotKey Probleme

  Alt 17. Jun 2015, 12:56
Wie kann ich der Ursache hier weiter auf den Grund gehen. Stehe hier erstmal auf dem Schlauch.
Ganz einfach mal an die Angaben von MS halten:
Zitat:
Return value

Type: BOOL

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.
(Hervorhebung von mir)

MfG Dalai
  Mit Zitat antworten Zitat
Sim_Star

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

AW: RegisterHotKey Probleme

  Alt 17. Jun 2015, 13:01
Ok ich bekomme für alle neuen Hotkeys die Fehlermeldung "unzulässige Attribute"
  Mit Zitat antworten Zitat
Benutzerbild von Dalai
Dalai

Registriert seit: 9. Apr 2006
1.680 Beiträge
 
Delphi 5 Professional
 
#7

AW: RegisterHotKey Probleme

  Alt 17. Jun 2015, 13:03
Ok ich bekomme für alle neuen Hotkeys die Fehlermeldung "unzulässige Attribute"
Dann solltest du mal die Eingabedaten prüfen, die an RegisterHotKey übergeben werden, d.h. da mal einen Breakpoint setzen und durchsteppen (F7/F8), und prüfen, ob du siehst, was du erwartest.

MfG Dalai
  Mit Zitat antworten Zitat
Photoner

Registriert seit: 6. Dez 2012
Ort: Nürnberg
103 Beiträge
 
Delphi 10.1 Berlin Starter
 
#8

AW: RegisterHotKey Probleme

  Alt 17. Jun 2015, 13:11
GetLastError liefert mir für alle Hotkeys außer den ersten:

Fehler: 1004

"ERROR_INVALID_FLAGS

1004 (0x3EC)

Invalid flags."

https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx

BOOL WINAPI RegisterHotKey(
_In_opt_ HWND hWnd,
_In_ int id,
_In_ UINT fsModifiers,
_In_ UINT vk
);

fsModifiers [in]

Type: UINT

The keys that must be pressed in combination with the key specified by the uVirtKey parameter in order to generate the WM_HOTKEY message. The fsModifiers parameter can be a
combination of the following values.
Value Meaning

MOD_ALT
0x0001



Either ALT key must be held down.

MOD_CONTROL
0x0002



Either CTRL key must be held down.

MOD_NOREPEAT
0x4000



Changes the hotkey behavior so that the keyboard auto-repeat does not yield multiple hotkey notifications.

Windows Vista: This flag is not supported.

MOD_SHIFT
0x0004



Either SHIFT key must be held down.

MOD_WIN
0x0008



Either WINDOWS key was held down. These keys are labeled with the Windows logo. Keyboard shortcuts that involve the WINDOWS key are reserved for use by the operating system.
Chris

Geändert von Photoner (17. Jun 2015 um 13:14 Uhr)
  Mit Zitat antworten Zitat
Photoner

Registriert seit: 6. Dez 2012
Ort: Nürnberg
103 Beiträge
 
Delphi 10.1 Berlin Starter
 
#9

AW: RegisterHotKey Probleme

  Alt 17. Jun 2015, 13:19
Key1 muss zwingend
  • CTRL
  • ALT
  • etc.
  • leer

sein. dann gehts:

Ergebnisse:

Memo1.Lines:

Memo1
HotKey.0 ---> CTRL + F9
Erfolg
49323
HotKey.1 ---> CTRL + F1
Erfolg
49328
HotKey.2 ---> + F2
Erfolg
49329
HotKey.3 ---> F3 +
Fehler: 1004
49266
HotKey.4 ---> F4 +
Fehler: 1004
49339
HotKey.5 ---> F5 +
Fehler: 1004
Chris
  Mit Zitat antworten Zitat
Benutzerbild von BUG
BUG

Registriert seit: 4. Dez 2003
Ort: Cottbus
2.094 Beiträge
 
#10

AW: RegisterHotKey Probleme

  Alt 17. Jun 2015, 13:21
Ergo: Die Zuordnung zu fsModifiers und vk für RegisterHotKey muss stimmen. Entweder du sortierst das oder es geht halt nicht anders.
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2   

Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 16:29 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz