shift and alt key lock

I would like to implement a button on my form that would be like a caps lock, but for shift, alt or ctrl. When on it would be like someone was holding that key down. How do I do it ?
ChLaAsked:
Who is Participating?
 
systanConnect With a Mentor Commented:
hi,

try to see the links;
http://delphi.about.com/cs/adptips2000/a/bltip0400_3.htm
http://www.delphicorner.f9.co.uk/articles/wapi13.htm
http://delphi.about.com/od/adptips2006/qt/caps_num_lock.htm
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
...
...
function CtrlDown : Boolean;
var
   State : TKeyboardState;
begin
   GetKeyboardState(State) ;
   Result := ((State[vk_Control] And 128) <> 0) ;
end;

function ShiftDown : Boolean;
var
   State : TKeyboardState;
begin
   GetKeyboardState(State) ;
   Result := ((State[vk_Shift] and 128) <> 0) ;
end;

function AltDown : Boolean;
var
   State : TKeyboardState;
begin
   GetKeyboardState(State) ;
   Result := ((State[vk_Menu] and 128) <> 0) ;
end;


procedure ToggleCapsLock;
var
  KeyState: TKeyboardState;
begin
  GetKeyboardState(KeyState);
  if (KeyState[VK_CAPITAL] = 0) then
  begin
    Keybd_Event(VK_CAPITAL, 1, KEYEVENTF_EXTENDEDKEY or 0, 0);
    Keybd_Event(VK_CAPITAL, 1, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);
  end
  else
  begin
    Keybd_Event(VK_CAPITAL, 0, KEYEVENTF_EXTENDEDKEY or 0, 0);
    Keybd_Event(VK_CAPITAL, 0, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if ( (CtrlDown) or (ShiftDown) or (AltDown) ) then
begin
ToggleCapsLock;
end;
end;

Open in new window

0
 
ChLaAuthor Commented:
What function would turn off shift and ctrl after locking one of them with

function CtrlDown : Boolean;
var
   State : TKeyboardState;
begin
   GetKeyboardState(State) ;
   Result := ((State[vk_Control] And 128) <> 0) ;
end;

or

function ShiftDown : Boolean;
var
   State : TKeyboardState;
begin
   GetKeyboardState(State) ;
   Result := ((State[vk_Shift] and 128) <> 0) ;
end;

0
 
systanCommented:
That function could not lock or disable Ctrl and Shift,  it only determines if Ctrl or Shift is pressed Down then capslock.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
systanCommented:
frm: expert Slick812; disables ctrl or shift keys


library BlockKeys;

uses
  Windows,
  Messages;

{$R *.RES}

var
hKeyHook: THandle = 0;
Hooked: Boolean = False;

function HookFunc(Code, VirtualKey, KeyStroke: Integer): Integer; stdcall;
begin
if  VirtualKey in [VK_TAB, VK_CONTROL, VK_MENU, VK_ESCAPE, VK_F1] then
  Result := 32
  else
  Result := CallNextHookEx(hKeyHook, Code, VirtualKey, KeyStroke);
end;


function StartHook: Boolean; export;
begin
Result := False;
if Hooked then
  begin
  Result := True;
  Exit;
  end;

hKeyHook := SetWindowsHookEx(WH_KEYBOARD, HookFunc, hInstance, 0);
if hKeyHook <> 0 then
  begin
  Result := True;
  Hooked := True;
  end;
end;


function StopHook: Boolean; export;
begin
if Hooked then
  Result := UnhookWindowsHookEx(hKeyHook)
  else
  Result := True;
if Result then Hooked := False;
end;

exports
  StartHook,
  StopHook;

begin

end.


 = = = = = = = = = = = = = = = = = = = = = = = = =

this may give you something to start with. . .
there are other examples in questions here at EE for key hooks.
  this seems to block some of the keys you asked, but it was not able to block some of the "System" key combos , like  Alt+F
0
 
ChLaAuthor Commented:
I am  not wanting to dissable the keys, or to find their state. I would like to lock them on. I would like to be able to make shift or control keys stay on as if being constantly pressed, like caps lock. Then later I want to be able to unlock them so they act normally again.
0
 
ChLaAuthor Commented:
I must be slow. I looked back at the reference to caps lock and num lock and applied it to shift and control, and it works. Thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.