JazzKeysUtils.pas


{************************************************************}
{                                                            }
{ Jazz SDK                                                   }
{ Jazz Core Library                                          }
{                                                            }
{ Copyright (c) 1998-2008                                    }
{ Cesar Romero <cesar@liws.com.br>                           }
{                                                            }
{************************************************************}
{ Reference                                                  }
{ http://www.delphifaq.com/faq/delphi_windows_API/f359.shtml }
{                                                            }
{************************************************************}
unit JazzKeysUtils;

interface

// Any side keys
function IsAltKeyDown: boolean;
function IsCtrlKeyDown: boolean;
function IsShiftKeyDown: boolean;

// Left side keys
function IsLeftAltKeyDown: boolean;
function IsLeftCtrlKeyDown: boolean;
function IsLeftShiftKeyDown: boolean;

// right side keys
function IsRightAltKeyDown: boolean;
function IsRightCtrlKeyDown: boolean;
function IsRightShiftKeyDown: boolean;

// Test if a key is the desired char ignoring case
function IsCharKey(Key, Value: Char): boolean; overload;
function IsCharKey(Key: Word; Value: Char): boolean; overload;

implementation

uses
  SysUtils, Windows;

function HighOrderBitSet(Value: Word): Boolean;
const
  HighOrderBit = 15;
type
  BitSet = set of 0..15;
begin
  HighOrderBitSet:= (HighOrderBit in BitSet(Value));
end;

function IsCharKey(Key, Value: Char): boolean;
begin
  Result:= UpCase(Key) = UpCase(Value);
end;

function IsCharKey(Key: Word; Value: Char): boolean;
begin
  Result:= IsCharKey(Char(Key), Value);
end;

function IsAltKeyDown: boolean;
begin
  Result:= HighOrderBitSet(Word(GetKeyState(VK_MENU)));
end;

function IsCtrlKeyDown: boolean;
begin
  Result:= HighOrderBitSet(Word(GetKeyState(VK_CONTROL)));
end;

function IsShiftKeyDown: boolean;
begin
  Result:= HighOrderBitSet(Word(GetKeyState(VK_SHIFT)));
end;

// Left
function IsLeftAltKeyDown: boolean;
begin
  Result:= HighOrderBitSet(Word(GetKeyState(VK_LMENU)));
end;

function IsLeftCtrlKeyDown: boolean;
begin
  Result:= HighOrderBitSet(Word(GetKeyState(VK_LCONTROL)));
end;

function IsLeftShiftKeyDown: boolean;
begin
  Result:= HighOrderBitSet(Word(GetKeyState(VK_LSHIFT)));
end;

// Right
function IsRightAltKeyDown: boolean;
begin
  Result:= HighOrderBitSet(Word(GetKeyState(VK_RMENU)));
end;

function IsRightCtrlKeyDown: boolean;
begin
  Result:= HighOrderBitSet(Word(GetKeyState(VK_RCONTROL)));
end;

function IsRightShiftKeyDown: boolean;
begin
  Result:= HighOrderBitSet(Word(GetKeyState(VK_RSHIFT)));
end;

end.