Delphi - Detect 3 keystrokes simultaneously

I want to detect the pressing of 3 keys in my form, for example Ctrl + C + N ... the typed form that I need to detect always starts with Ctrl , and then two letters go.

How am i doing this?

+6
source share
2 answers

Upon the arrival of one of the keys, you can see if the other key has already been disabled. For instance:.

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Shift = [ssCtrl] then begin case Key of Ord('C'): if (GetKeyState(Ord('N')) and $80) = $80 then ShowMessage('combo'); Ord('N'): if (GetKeyState(Ord('C')) and $80) = $80 then ShowMessage('combo'); end; end; end; 


However, it will also detect, for example, N + Ctrl + C , a sequence that does not start with the Ctrl key. If this does not apply to a valid key combination, you can save a little key history with a flag. The following should only detect sequences that initially begin with Ctrl :

 type TForm1 = class(TForm) procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); private FValidKeyCombo: Boolean; end; ... procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if FValidKeyCombo and (Shift = [ssCtrl]) then case Key of Ord('C'): if (GetKeyState(Ord('N')) and $80) = $80 then ShowMessage('combo'); Ord('N'): if (GetKeyState(Ord('C')) and $80) = $80 then ShowMessage('combo'); end; FValidKeyCombo := (Shift = [ssCtrl]) and (Key in [Ord('C'), Ord('N')]); end; procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); begin FValidKeyCombo := False; end; 
+16
source

There is an easier way:

  procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin If (GetKeyState(Ord('Q'))<0) and (GetKeyState(Ord('N'))<0) and (GetKeyState(VK_CONTROL)<0) Then ShowMessage('You did it :)'); End; 
+4
source

All Articles