Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#6

AW: Programm mit gedrückter Shift-Taste starten

  Alt 7. Mär 2015, 13:18
Ich weiß, ist primitiv, aber funzt. SirRufo würde hierfür vermutlich eine Klasse basteln. (@SirRufo: ist jetzt nicht bös gemeint!) Wäre programmiertechnisch auch sinnvoller, keine Frage, aber das ist mir in diesem Fall ehrlich gesagt so was von...
Schon ok, aber ja, ich würde das in eine Klasse (Struktur) packen, die dann den Zugriff vereinfacht.
Delphi-Quellcode:
unit KeyStateSnapshot;

interface

uses
  Winapi.Windows;

type
  TKeyState = ( ksPressed, ksToggled );
  TKeyStates = set of TKeyState;

  IKeyStateSnapshot = interface
    ['{83675AEC-1F42-44CD-84F3-0B7EED2EE186}']
    function GetKeyStates( Key: Byte ): TKeyStates;
    property KeyStates[Key: Byte]: TKeyStates read GetKeyStates; default;
  end;

  TKeyStateSnapshot = class( TInterfacedObject, IKeyStateSnapshot )
  private const
    BITMASK_KEY_ISPRESSED = $80;
    BITMASK_KEY_ISTOGGLED = $01;
  private
    class var _Startup: IKeyStateSnapshot;
    class constructor Create;
  private
    FKeyboardState: TKeyboardState;
    function GetKeyStates( Key: Byte ): TKeyStates;
  protected
    constructor Create;
  public
    class function Current: IKeyStateSnapshot;
    class function Startup: IKeyStateSnapshot;
  end;

implementation

{ TKeyStateSnapshot }

class constructor TKeyStateSnapshot.Create;
begin
  _Startup := TKeyStateSnapshot.Create;
end;

constructor TKeyStateSnapshot.Create;
var
  LResult: LongBool;
begin
  inherited Create;
  LResult := GetKeyboardState( FKeyboardState );
end;

class function TKeyStateSnapshot.Current: IKeyStateSnapshot;
begin
  Result := TKeyStateSnapshot.Create;
end;

function TKeyStateSnapshot.GetKeyStates( Key: Byte ): TKeyStates;
var
  LState: Byte;
begin
  Result := [];
  LState := FKeyboardState[Key];
  if ( LState and BITMASK_KEY_ISPRESSED ) = BITMASK_KEY_ISPRESSED
  then
    Result := Result + [ksPressed];
  if ( LState and BITMASK_KEY_ISTOGGLED ) = BITMASK_KEY_ISTOGGLED
  then
    Result := Result + [ksToggled];
end;

class function TKeyStateSnapshot.Startup: IKeyStateSnapshot;
begin
  Result := _Startup;
end;

end.
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat