Solved

Disabiling Alt-Tab

Posted on 2003-11-06
3
288 Views
Last Modified: 2010-04-05
Hey,

How would I disable Alt-Tab key function?

Cheers
0
Comment
Question by:computerprogramer
3 Comments
 
LVL 2

Expert Comment

by:gangsterboss
ID: 9698089
this disables all windows keys even once the program is closed

uses winprocs;
procedure TForm1.FormCreate(Sender: TObject);
var
   Dummy : integer;
begin
  Dummy := 0;
  SystemParametersInfo( SPI_SCREENSAVERRUNNING, 1, @Dummy, 0);
end;

if u would like to get them working its exactly the same code but replace the 1 with a 0

var
   Dummy : integer;
begin
  Dummy := 0;
  SystemParametersInfo( SPI_SCREENSAVERRUNNING, 0, @Dummy, 0);
end;
0
 
LVL 5

Accepted Solution

by:
snehanshu earned 20 total points
ID: 9698244
SPI_SCREENSAVERRUNNING would not work under win NT.
You can achieve this blocking using a "Keyboard Hook". A hook can be implemented using a DLL, but you could also set a hook within your program wiyhout a DLL.
Here's a code that blocks Alt-Tab when you press one button, and stops blocking Alt-Tab when you press the other button.
It can provide other functionalities if you uncomment some code in KeyBoardProc.
enjoy...

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;
const
  WH_KEYBOARD_LL = 13;
  LLKHF_EXTENDED = $01;
  LLKHF_INJECTED = $10;
  LLKHF_ALTDOWN  = $20;
  LLKHF_UP       = $80;

type
  TForm1 = class(TForm)
    BlockBtn: TButton;
    ReleaseBtn: TButton;

    procedure BlockBtnClick(Sender: TObject);
    procedure ReleaseBtnClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);  private
    procedure LockKeyboard; stdcall;
    procedure UnlockKeyboard; stdcall;
    { Private declarations }
  public
    { Public declarations }
  end;
 TKBDLLHOOKSTRUCT = packed record
   vkCode:      DWORD;
   scanCode:    DWORD;
   flags:       DWORD;
   time:        DWORD;
   dwExtraInfo: DWORD;
 end;
 PKBDLLHOOKSTRUCT = ^TKBDLLHOOKSTRUCT;
var
  Form1: TForm1;
  hKbdProc: HHOOK;


implementation

{$R *.DFM}

{ TForm1 }

function KeyBoardProc(iCode: Integer; wParam, lParam: LongInt): LongInt; stdcall;
var
  KeyData: TKBDLLHOOKSTRUCT;
  bIgnore: Boolean;
begin
  bIgnore := False;
  if iCode = HC_ACTION then
  begin
    KeyData := PKBDLLHOOKSTRUCT(lParam)^;
    with KeyData do
    begin
      // Block Control-Escape
//      if (vkCode = VK_ESCAPE) and ((GetAsyncKeyState(VK_CONTROL) and $8000) <> 0) then
//        bIgnore := True;
      // Block Control-Tab
//      if (vkCode = VK_TAB) and ((GetAsyncKeyState(VK_CONTROL) and $8000) <> 0) then
//        bIgnore := True;
      // Block Alt-Tab
      if (vkCode = VK_TAB) and ((flags and LLKHF_ALTDOWN) <> 0) then
        bIgnore := True;
      // Block Alt-Escape
//      if (vkCode = VK_ESCAPE) and ((flags and LLKHF_ALTDOWN) <> 0) then
//        bIgnore := True;
     // Block Left/Right Windows key
//      if KeyData.vkCode in [VK_LWIN, VK_RWIN] then
//        bIgnore := True;
      // Block Control-F4
//      if (vkCode = VK_F4) and ((GetAsyncKeyState(VK_CONTROL) and $8000) <> 0) then
//        bIgnore := True;
      // Block Alt-F4
//      if (vkCode = VK_F4) and ((flags and LLKHF_ALTDOWN) <> 0) then
//        bIgnore := True;
    end;
  end;
  if bIgnore then
    Result := 1
  else
    Result := CallNextHookEx(hKbdProc, iCode, wParam, lParam);
end;

procedure TForm1.LockKeyboard;
begin
  hKbdProc := SetWindowsHookEx(WH_KEYBOARD_LL, KeyBoardProc, HInstance, 0);
end;

procedure TForm1.UnlockKeyboard;
begin
  if hKbdProc <> 0 then
  begin
    UnHookWindowsHookEx(hKbdProc);
    hKbdProc := 0;
  end;
end;

procedure TForm1.BlockBtnClick(Sender: TObject);
begin
  LockKeyboard;
end;

procedure TForm1.ReleaseBtnClick(Sender: TObject);
begin
  UnlockKeyboard;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  UnlockKeyboard;
end;

end.
0
 

Expert Comment

by:huiyue
ID: 9699769
Yes,just use keyboard Hook
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question