Solved

Disabiling Alt-Tab

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

How would I disable Alt-Tab key function?

Cheers
0
Comment
Question by:computerprogramer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

751 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