Fórum Bloquear as teclas CTRL + TAB #469174
07/02/2014
0
Alguém sabe como faço para bloquear as teclas CTRL+TAB?
Pesquisei vários exemplos na internet e não encontrei alguma função que rodasse no Windows 7 ou superior.
Alessandro Yamasaki
Curtir tópico
+ 0Post mais votado
10/02/2014
Ai já é um pouco mais complicado, veja esta unit abaixo, tem algumas das principais funções de teclas do windows. Crie ela em seu projeto.
unit unDisableWinKeys;
interface
uses Windows;
type
PKBDLLHOOKSTRUCT = ^TKBDLLHOOKSTRUCT;
TKBDLLHOOKSTRUCT = packed record
vkCode: DWORD;
scanCode: DWORD;
flags: DWORD;
time: DWORD;
dwExtraInfo: DWORD;
end;
function DisableWindowsUI: Boolean;
function EnableWindowsUI: Boolean;
function IsWindowsUIDisabled: Boolean;
const
WH_KEYBOARD_LL = 13;
const
LLKHF_ALTDOWN = $0020;
var
hKeyboardHook: HHOOK = 0;
implementation
function LowLevelKeyboardHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM): HRESULT; stdcall;
var
pkbhs: PKBDLLHOOKSTRUCT;
begin
pkbhs := PKBDLLHOOKSTRUCT(lParam);
if nCode = HC_ACTION then
begin
// Disable CTRL+ESC
if (pkbhs^.vkCode = VK_ESCAPE) and WordBool(GetAsyncKeyState(VK_CONTROL) and $8000) then
begin
Result := 1;
Exit;
end;
// Disable ALT+TAB
if (pkbhs^.vkCode = VK_TAB) and LongBool(pkbhs^.flags and LLKHF_ALTDOWN) then
begin
Result := 1;
Exit;
end;
// Disable ALT+ESC
if (pkbhs^.vkCode = VK_ESCAPE) and LongBool(pkbhs^.flags and LLKHF_ALTDOWN) then
begin
Result := 1;
Exit;
end;
//=============START:modified=====================================================
// Disable CTRL+ENTER
if (pkbhs^.vkCode = VK_RETURN) and LongBool(pkbhs^.flags and LLKHF_ALTDOWN) then
begin
Result := 1;
Exit;
end;
// Disable winkey(Left / Right)
if (pkbhs^.vkCode = VK_LWIN) or (pkbhs^.vkCode = VK_RWIN) then
begin
Result := 1;
Exit;
end;
// Disable menukey(Left/Right)
if (pkbhs^.vkCode = VK_MENU) or (pkbhs^.vkCode = VK_LMENU) or (pkbhs^.vkCode = VK_RMENU) then
begin
Result := 1;
Exit;
end;
//==================END:Modified================================================================
end;
Result := CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
end;
function DisableWindowsUI: Boolean;
begin
if hKeyboardHook = 0 then
hKeyboardHook := SetWindowsHookEx(WH_KEYBOARD_LL, @LowLevelKeyboardHook, HInstance, 0);
Result := (hKeyboardHook <> 0)
end;
function EnableWindowsUI: Boolean;
begin
Result := False;
if (hKeyboardHook <> 0) and UnhookWindowsHookEx(hKeyboardHook) then
begin
hKeyboardHook := 0;
Result := True;
end;
end;
function IsWindowsUIDisabled: Boolean;
begin
Result := (hKeyboardHook <> 0)
end;
end.
No seu formulário, coloque ela na uses:
uses unDisableWinKeys;
ai basta chamar a função, aonde você quiser para desabilitar as teclas (inclusive o alt-tab), neste caso criei um botão:
procedure TFrmLogin.Button1Click(Sender: TObject); begin DisableWindowsUI(); end;
enfim, nesta unit tem vários exemplos (alt-tab, ctrl-tab, alt-esc ...), você pode criar uma função apenas para o ALT+TAB, ok!
se precisar habilitar novamente, basta chamar o : EnableWindowsUI();
e também pode criar uma função para o ctrl+tab (da primeira mensagem) para não ter que ficar tratando em 2 lugares distintos - pode deixar tudo na unit:
if (pkbhs^.vkCode = VK_TAB) and WordBool(GetAsyncKeyState(VK_CONTROL) and $8000) then
begin
Result := 1;
Exit;
end;
abraços,
luiz
Luiz
Gostei + 1
Mais Posts
07/02/2014
Luiz
você pode adicionar o componente TApplicationEvents da paleta Additional e no evento OnMessage deste componente
procedure TFrmLogin.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
var
State: TKeyboardState;
begin
testa := false;
GetKeyboardState(State);
if ((State[VK_CONTROL] and 128)<>0) And (Msg.wParam = VK_TAB) then
begin
ShowMessage('apertou ctrl tab');
testa := true;
end;
end;
ai pode fazer a função que quiser quando testa = true;
Gostei + 0
07/02/2014
Alessandro Yamasaki
Excelente dica Luiz Coelho
Resolvido e obrigado
Gostei + 0
10/02/2014
Alessandro Yamasaki
Gostei + 0
10/02/2014
Alessandro Yamasaki
Gostei + 0
10/02/2014
Luiz
Qlqr coisa to a disposição.
Gostei + 0
06/03/2020
Anderson Gonçalves
Eu vi todas as principais teclas menos o DELETE como ficaria o CTRL+AL+DELETE para impedir que o usuário abra.
Abraço irmão, parabéns pelo conhecimento e obrigado por dividir conosco.
Ai já é um pouco mais complicado, veja esta unit abaixo, tem algumas das principais funções de teclas do windows. Crie ela em seu projeto.
unit unDisableWinKeys;
interface
uses Windows;
type
PKBDLLHOOKSTRUCT = ^TKBDLLHOOKSTRUCT;
TKBDLLHOOKSTRUCT = packed record
vkCode: DWORD;
scanCode: DWORD;
flags: DWORD;
time: DWORD;
dwExtraInfo: DWORD;
end;
function DisableWindowsUI: Boolean;
function EnableWindowsUI: Boolean;
function IsWindowsUIDisabled: Boolean;
const
WH_KEYBOARD_LL = 13;
const
LLKHF_ALTDOWN = $0020;
var
hKeyboardHook: HHOOK = 0;
implementation
function LowLevelKeyboardHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM): HRESULT; stdcall;
var
pkbhs: PKBDLLHOOKSTRUCT;
begin
pkbhs := PKBDLLHOOKSTRUCT(lParam);
if nCode = HC_ACTION then
begin
// Disable CTRL+ESC
if (pkbhs^.vkCode = VK_ESCAPE) and WordBool(GetAsyncKeyState(VK_CONTROL) and $8000) then
begin
Result := 1;
Exit;
end;
// Disable ALT+TAB
if (pkbhs^.vkCode = VK_TAB) and LongBool(pkbhs^.flags and LLKHF_ALTDOWN) then
begin
Result := 1;
Exit;
end;
// Disable ALT+ESC
if (pkbhs^.vkCode = VK_ESCAPE) and LongBool(pkbhs^.flags and LLKHF_ALTDOWN) then
begin
Result := 1;
Exit;
end;
//=============START:modified=====================================================
// Disable CTRL+ENTER
if (pkbhs^.vkCode = VK_RETURN) and LongBool(pkbhs^.flags and LLKHF_ALTDOWN) then
begin
Result := 1;
Exit;
end;
// Disable winkey(Left / Right)
if (pkbhs^.vkCode = VK_LWIN) or (pkbhs^.vkCode = VK_RWIN) then
begin
Result := 1;
Exit;
end;
// Disable menukey(Left/Right)
if (pkbhs^.vkCode = VK_MENU) or (pkbhs^.vkCode = VK_LMENU) or (pkbhs^.vkCode = VK_RMENU) then
begin
Result := 1;
Exit;
end;
//==================END:Modified================================================================
end;
Result := CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
end;
function DisableWindowsUI: Boolean;
begin
if hKeyboardHook = 0 then
hKeyboardHook := SetWindowsHookEx(WH_KEYBOARD_LL, @LowLevelKeyboardHook, HInstance, 0);
Result := (hKeyboardHook <> 0)
end;
function EnableWindowsUI: Boolean;
begin
Result := False;
if (hKeyboardHook <> 0) and UnhookWindowsHookEx(hKeyboardHook) then
begin
hKeyboardHook := 0;
Result := True;
end;
end;
function IsWindowsUIDisabled: Boolean;
begin
Result := (hKeyboardHook <> 0)
end;
end.
No seu formulário, coloque ela na uses:
uses unDisableWinKeys;
ai basta chamar a função, aonde você quiser para desabilitar as teclas (inclusive o alt-tab), neste caso criei um botão:
procedure TFrmLogin.Button1Click(Sender: TObject); begin DisableWindowsUI(); end;
enfim, nesta unit tem vários exemplos (alt-tab, ctrl-tab, alt-esc ...), você pode criar uma função apenas para o ALT+TAB, ok!
se precisar habilitar novamente, basta chamar o : EnableWindowsUI();
e também pode criar uma função para o ctrl+tab (da primeira mensagem) para não ter que ficar tratando em 2 lugares distintos - pode deixar tudo na unit:
if (pkbhs^.vkCode = VK_TAB) and WordBool(GetAsyncKeyState(VK_CONTROL) and $8000) then
begin
Result := 1;
Exit;
end;
abraços,
luiz
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)