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.