Solved

Disabiling Alt-Tab

Posted on 2003-11-06
3
284 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now