FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Deshabilitar las teclas de windows + alt-tab ctrl-esc
Posts: 140
Joined: Thu Feb 02, 2006 12:09 PM
Deshabilitar las teclas de windows + alt-tab ctrl-esc
Posted: Mon Sep 28, 2009 06:33 AM
Saludos ,
Necesito desde FWH o registro como deshabilitar las teclas windows, alt-tab, ctrl-esc,
consegui un codigo en VB aqui lo dejo a ver si uno de ustedes pudiera descifrarlo un poco.
Gracias de antemano.
Code (fw): Select all Collapse
'------------------------------------------------------------------------------
' Para bloquear algunas teclas en Windows NT/2000/XP                (08/Mar/03)
' Para NT debe tener el SP3 como mínimo
'
' ¡¡¡ NO FUNCIONA para Ctrl+Alt+Supr !!!
'
' En este ejemplo se bloquean las siguientes teclas:
'   Ctrl+Esc, Alt+Tab y Alt+Esc
'
' Convertido a Visual Basic .NET 2003                               (15/Oct/04)
' Para una petición de Nathaly en el grupo public.es.csharp
'
' ©Guillermo 'guille' Som, 2003-2004
'------------------------------------------------------------------------------
Option Strict On
Option Explicit On 

Imports System
Imports Microsoft.VisualBasic
Imports System.Runtime.InteropServices

Module MBloquearTeclas
    ' para guardar el gancho creado con SetWindowsHookEx
    Private mHook As Integer
    '
    ' para indicar a SetWindowsHookEx que tipo de gancho queremos instalar
    Private Const WH_KEYBOARD_LL As Integer = 13
    ' este es para el ratón
    'Private Const WH_MOUSE_LL As Long = 14&
    '
    Private Structure tagKBDLLHOOKSTRUCT
        Dim vkCode As Integer
        Dim scanCode As Integer
        Dim flags As Integer
        Dim time As Integer
        Dim dwExtraInfo As Integer
    End Structure
    '
    Private Const VK_TAB As Integer = &H9
    Private Const VK_CONTROL As Integer = &H11 ' tecla Ctrl
    'Private Const VK_MENU As Long = &H12        ' tecla Alt
    Private Const VK_ESCAPE As Integer = &H1B
    'Private Const VK_DELETE As Integer = &H2E      ' tecla Supr (Del)
    '
    Private Const LLKHF_ALTDOWN As Integer = &H20
    '
    ' códigos para los ganchos (la acción a tomar en el gancho del teclado)
    Private Const HC_ACTION As Integer = 0
    '
    ' Funciones del API de Windows
    '-----------------------------
    ' para asignar un gancho (hook)
    Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" _
            (ByVal idHook As Integer, ByVal lpfn As LLKeyBoardProcDelegate, _
            ByVal hMod As Integer, ByVal dwThreadId As Integer) As Integer
    ' para quitar el gancho creado con SetWindowsHookEx
    Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Integer) As Integer
    ' para llamar al siguiente gancho
    Private Declare Function CallNextHookEx Lib "user32" _
            (ByVal hHook As Integer, ByVal nCode As Integer, _
            ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    ' para saber si se ha pulsado en una tecla
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Short
    '
    ' El delegado para usar con AddressOf
    Private Delegate Function LLKeyBoardProcDelegate(ByVal nCode As Integer, _
            ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    '
    ' La función a usar para el gancho del teclado
    Private Function LLKeyBoardProc(ByVal nCode As Integer, _
            ByVal wParam As Integer, ByVal lParam As Integer) As Integer
        Dim pkbhs As tagKBDLLHOOKSTRUCT
        Dim ret As Integer = 0
        '
        ' copiar el parámetro en la estructura
        pkbhs = CType(Marshal.PtrToStructure(New IntPtr(lParam), pkbhs.GetType), tagKBDLLHOOKSTRUCT)
        '
        If nCode = HC_ACTION Then
            '
            ' si se pulsa Ctrl+Esc
            If pkbhs.vkCode = VK_ESCAPE Then
                If (GetAsyncKeyState(VK_CONTROL) And &H8000S) <> 0 Then
                    ret = 1
                End If
            End If
            '
            ' si se pulsa Alt+Tab
            If pkbhs.vkCode = VK_TAB Then
                If (pkbhs.flags And LLKHF_ALTDOWN) <> 0 Then
                    ret = 1
                End If
            End If
            '
            ' si se pulsa Alt+Esc
            If pkbhs.vkCode = VK_ESCAPE Then
                If (pkbhs.flags And LLKHF_ALTDOWN) <> 0 Then
                    ret = 1
                End If
            End If
            '
            '' si se pulsa Alt+Supr
            '' (esto no funciona con Ctrl)
            'If pkbhs.vkCode = VK_DELETE Then
            '    If (pkbhs.flags And LLKHF_ALTDOWN) <> 0 Then
            '        ret = 1
            '    End If
            'End If
        End If
        '
        If ret = 0 Then
            ret = CallNextHookEx(mHook, nCode, wParam, lParam)
        End If
        '
        Return ret
        '
    End Function
    '
    Public Sub HookKeyB(ByVal hMod As Integer)
        ' instalar el gancho para el teclado
        ' hMod será el valor de App.hInstance de la aplicación
        mHook = SetWindowsHookEx(WH_KEYBOARD_LL, New LLKeyBoardProcDelegate(AddressOf LLKeyBoardProc), hMod, 0)
    End Sub
    '
    Public Sub UnHookKeyB()
        ' desinstalar el gancho para el teclado
        ' Es importante hacerlo antes de finalizar la aplicación,
        ' normalmente en el evento Unload o QueryUnload
        If mHook <> 0 Then
            UnhookWindowsHookEx(mHook)
        End If
    End Sub
End Module
Mario Antonio González Osal

Venezuela

m a g 0 7 1 @ g m a i l. c o m
Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: Deshabilitar las teclas de windows + alt-tab ctrl-esc
Posted: Mon Sep 28, 2009 12:10 PM
Code (fw): Select all Collapse
/*
06/07/07 - Desativar teclas especiais 
  Olá,

Neste exemplo que fiz associado a dll que é encontrada no endereço:

<!-- m --><a class="postlink" href="http://www.codeproject.com/win32/AntonioWinLock.asp">http://www.codeproject.com/win32/AntonioWinLock.asp</a><!-- m -->

...é possível fazer o seguinte em FWH:

- Ativa/Desativar alt+tab
- Ativa/Desativar alt+esc
- Ativa/Desativar ctrl+alt+del
- Mostrar/Esconder a barra de tarefas
- Mostrar/Esconder o desktop
- Mostrar/Esconder o Start Button
- Mostrar/Esconder o System Clock
- Rodar um processo em outro desktop

Espero que gostem :-)

Abraços,

Rossine.
 

esta em ALTTAB.ZIP em dicas mais dicas

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

En: 21/08/2008 - Manuel Mercado Gentilmente Converteu Para FIVEWIN FOR XHARBOUR

Hola João

Una conversión rápida del ejemplo de Rossine a su versión en C directamente
con FWH sin Dll's
Manuel Mercado
*/

#Include "FiveWin.Ch"
#Include "Dll.Ch"

Function Teclado() //Main()

/*
   MsgStop( StartButton( .F. ), "Desabilitando o botão Start(Iniciar) - Win 9x, NT, 2K, XP" )

   MsgStop( StartButton( .T. ), "Habilitando   o botão Start(Iniciar) - Win 9x, NT, 2K, XP" )

   MsgStop( ShowDesktop( .F. ), "Desabilitando o Desktop - Win 9x, NT, 2K, XP" )

   MsgStop( DesktopProc( "MyDesktop2", "Calc.exe" ), "Executando uma tarefa em outro desktop - Win NT, 2K, XP" )

   MsgStop( ShowDesktop( .T. ), "Habilitando o Desktop - Win 9x, NT, 2K, XP" )

   MsgStop( ShowTaskbar( .F. ), "Desabilitando o Taskbar(Barra de Tarefas) - Win 9x, NT, 2K, XP" )

   MsgStop( ShowTaskbar( .T. ), "Habilitando   o Taskbar(Barra de Tarefas) - Win 9x, NT, 2K, XP" )

   MsgStop( ShowClock( .F. ), "Desabilitando o Clock(Relogio) - Win 9x, NT, 2K, XP" )

   MsgStop( ShowClock( .T. ), "Habilitando o Clock(Relogio) - Win 9x, NT, 2K, XP" )

   MsgStop( AltTabEnable( 0, .F. ), "Desabilitando ALT+TAB e ALT+ESC - Windows NT, 2k" )

   MsgStop( AltTabEnable( 0, .T. ), "Habilitando   ALT+TAB e ALT+ESC - Windows NT, 2k" )

   // Para Windows XP/NT e 2000 / 2003
   IF IsWinNT() .OR. IsWin2000()  //??? Nao testei em 2000/2003

       MsgStop( CtrlAltDel( .F. ), "Desabilitando o Ctrl+Alt+Del - Windows NT, 2k" )

       MsgStop( CtrlAltDel( .T. ), "Habilitando   o Ctrl+Alt+Del - Windows NT, 2k" )

   ELSE  //-> Windows 95/98 - Millenium Edition

      Ctrl_Alt_Del( .f. )

      MsgAlert( "CTRL+ALT+DEL DESLIGADO" + CRLF + "Please, press CTRL + ALT + DEL", "Desligado" )

      Ctrl_Alt_Del( .t. )

      MsgAlert( "CTRL+ALT+DEL LIGADO" + CRLF + "Please, press CTRL + ALT + DEL", "Ligado" )

   ENDIF
*/

Return Nil
//
//-> Comentario: Simplesmente, FANTASTICO!!!
//
//-> Complementando para Windows 95/98 Milleniun Edition
//
// Antonio Carlos Pantaglione
// <!-- e --><a href="mailto:Toninho@fwi.com.br">Toninho@fwi.com.br</a><!-- e -->
// Agosto/2001
//

//#include "fivewin.ch"
//#include "dll.ch"

//----------------------------------------------------------------------------//

//-> Modifiquei para nao ter choque com a funcao par o Windows NT(XP)
Procedure Ctrl_Alt_Del( lState )

   if !lState
      SysParInfo( 97, 1, 0, 0 )
   else
      SysParInfo( 97, 0, 0, 0 )
   endif

return Nil

//----------------------------------------------------------------------------//

dll32 static function SysParInfo( uAction AS LONG, uParam AS LONG, vParam AS LONG, uWinIni AS LONG ) ;
      AS LONG PASCAL  FROM "SystemParametersInfoA" LIB "User32.dll"

//----------------------------------------------------------------------------//

//-> Rotina em C -> By Manuel Mercado - The Best

#pragma BEGINDUMP

#include    <windows.h>
#include    <stdlib.h>
#include    <hbapi.h>
#define     ID_STARTBUTTON  0x130                  // Start button ID
#define     ID_TRAY         0x12F                  // System tray ID
#define     ID_CLOCK        0x12F                  // System clock ID
#define     PROGRAM_MANAGER "Program Manager"   // Program manager window name
#define     TASKBAR         "Shell_TrayWnd"     // Taskbar class name

int WINAPI TaskManager_Enable_Disable( BOOL ) ;
int WINAPI StartButton_Show_Hide( BOOL ) ;
int WINAPI Desktop_Show_Hide( BOOL ) ;
int WINAPI Process_Desktop(char *, char * ) ;
int WINAPI Taskbar_Show_Hide( BOOL ) ;
int WINAPI Clock_Show_Hide( BOOL ) ;
int WINAPI AltTab_Enable_Disable( HWND, BOOL ) ;
BOOL StartProcess( char *, char * ) ;
LRESULT CALLBACK LowLevelMouseHookProc( int nCode, WORD wParam, DWORD lParam ) ;
LRESULT CALLBACK MouseHookProc( int nCode, WORD wParam, DWORD lParam ) ;

HHOOK       hHook ;          // Mouse hook
HINSTANCE   hInst ;         // Instance handle

HB_FUNC( ALTTABENABLE )
{
   HWND hWnd    = (HWND ) hb_parnl( 1 ) ;
   BOOL bEnable = hb_parl( 2 ) ;
   int iRet ;

   iRet = AltTab_Enable_Disable( hWnd, bEnable ) ;
   hb_retni( iRet ) ;
}

HB_FUNC( SHOWCLOCK )
{
   BOOL bShow = hb_parl( 1 ) ;
   int iRet ;

   iRet = Clock_Show_Hide( bShow ) ;
   hb_retni( iRet ) ;
}

HB_FUNC( SHOWTASKBAR )
{
   BOOL bShow = hb_parl( 1 ) ;
   int iRet ;

   iRet = Taskbar_Show_Hide( bShow ) ;
   hb_retni( iRet ) ;
}

HB_FUNC( DESKTOPPROC )
{
   char* c1 = hb_parc( 1 ) ;
   char* c2 = hb_parc( 2 ) ;
   int iRet ;

   iRet = Process_Desktop( c1, c2 ) ;
   hb_retni( iRet ) ;
}

HB_FUNC( SHOWDESKTOP )
{
   BOOL bShow = hb_parl( 1 ) ;
   int iRet ;

   iRet = Desktop_Show_Hide( bShow ) ;
   hb_retni( iRet ) ;
}

HB_FUNC( STARTBUTTON )
{
   BOOL bShow = hb_parl( 1 ) ;
   int iRet ;

   iRet = StartButton_Show_Hide( bShow ) ;
   hb_retni( iRet ) ;
}

HB_FUNC( CTRLALTDEL )
{
   BOOL bEnable = hb_parl( 1 ) ;
   int iRet ;

   iRet = TaskManager_Enable_Disable( bEnable ) ;
   hb_retni( iRet ) ;
}

int WINAPI TaskManager_Enable_Disable(BOOL bEnableDisable)
{
    #define KEY_DISABLETASKMGR  "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"
    #define VAL_DISABLETASKMGR  "DisableTaskMgr"

    HKEY    hKey;
    DWORD   val;
    LONG    r;

    if (RegOpenKey(HKEY_CURRENT_USER, KEY_DISABLETASKMGR, &hKey) != ERROR_SUCCESS)
        if (RegCreateKey(HKEY_CURRENT_USER, KEY_DISABLETASKMGR, &hKey) != ERROR_SUCCESS)
            return 0;

    if (bEnableDisable) // Enable
    {
        r = RegDeleteValue(hKey, VAL_DISABLETASKMGR);
    }
    else                // Disable
    {
        val = 1;
        r = RegSetValueEx(hKey, VAL_DISABLETASKMGR, 0, REG_DWORD, (BYTE *)&val, sizeof(val));
    }

    RegCloseKey(hKey);

    return (r == ERROR_SUCCESS ? 1 : 0) ;
}

int WINAPI StartButton_Show_Hide( BOOL bShowHide )
{
    HWND    hWnd;

    hWnd = GetDlgItem( FindWindow( TASKBAR, NULL ), ID_STARTBUTTON ) ;
    if ( hWnd == NULL )
       return 0 ;

    ShowWindow( hWnd, bShowHide ? SW_SHOW : SW_HIDE) ;
    UpdateWindow( hWnd );

    return 1 ;
}

int WINAPI Taskbar_Show_Hide( BOOL bShowHide )
{
    HWND    hWnd;

    hWnd = FindWindow( TASKBAR, NULL ) ;
    if ( hWnd == NULL )
        return 0 ;

    ShowWindow( hWnd, bShowHide ? SW_SHOW : SW_HIDE ) ;
    UpdateWindow(hWnd) ;

    return 1 ;
}

int WINAPI Desktop_Show_Hide( BOOL bShowHide )
{
    OSVERSIONINFO   osvi ;
    BOOL            bIsWindowsNT4SP3orLater ;
    int             iServicePack ;
    char           *p ;

    // Determine the current windows version
    osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO ) ;
    GetVersionEx( &osvi );
    for (p = osvi.szCSDVersion; *p  && ! isdigit( *p ) ; *p++ ) ;
    iServicePack = atoi( p ) ;
    bIsWindowsNT4SP3orLater = ( osvi.dwPlatformId == VER_PLATFORM_WIN32_NT ) &&
                               ( ( ( osvi.dwMajorVersion == 4 ) && ( iServicePack >= 3 ) ) ||
                                     ( osvi.dwMajorVersion > 4 ) ) ;

    if ( ! bShowHide )
    {
        if ( ! hHook )
        {
            hHook  = SetWindowsHookEx( bIsWindowsNT4SP3orLater ? WH_MOUSE_LL : WH_MOUSE,
                                      bIsWindowsNT4SP3orLater ? ( HOOKPROC )LowLevelMouseHookProc : ( HOOKPROC )MouseHookProc,
                             hInst, 0 ) ;
            if ( ! hHook )
                return 0 ;
        }
    }
    else
    {
        UnhookWindowsHookEx(hHook);
        hHook = NULL;
    }

    return SetWindowPos( FindWindow( NULL, PROGRAM_MANAGER ), NULL, 0, 0, 0, 0,
                        bShowHide ? SWP_SHOWWINDOW : SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER);
}

LRESULT CALLBACK LowLevelMouseHookProc( int nCode, WORD wParam, DWORD lParam )
{
    PMSLLHOOKSTRUCT p = (PMSLLHOOKSTRUCT)lParam ;
    HWND hWnd = WindowFromPoint( p->pt ) ;

    if( nCode >= 0 )
    {
        if ( ( wParam == WM_LBUTTONDOWN || wParam == WM_RBUTTONDOWN ) && hWnd == GetDesktopWindow() )
        {
            return 1 ;
        }
    }

    return CallNextHookEx( hHook, nCode, wParam, lParam ) ;
}

LRESULT CALLBACK MouseHookProc( int nCode, WORD wParam, DWORD lParam )
{
    if( nCode >= 0 )
    {
        if ( wParam == WM_LBUTTONDBLCLK )
        {
            if ( ( ( MOUSEHOOKSTRUCT * )lParam )->hwnd == GetDesktopWindow() )
            {
                return 1 ;
            }
        }
    }

    return CallNextHookEx( hHook, nCode, wParam, lParam ) ;
}

int WINAPI Process_Desktop(char *szDesktopName, char *szPath )
{
    HDESK   hOriginalThread ;
    HDESK   hOriginalInput ;
    HDESK   hNewDesktop ;

    // Save original ...
    hOriginalThread = GetThreadDesktop( GetCurrentThreadId() ) ;
    hOriginalInput = OpenInputDesktop( 0, FALSE, DESKTOP_SWITCHDESKTOP ) ;

    // Create a new Desktop and switch to it
    hNewDesktop = CreateDesktop( szDesktopName, NULL, NULL, 0, GENERIC_ALL, NULL ) ;
    SetThreadDesktop( hNewDesktop ) ;
    SwitchDesktop( hNewDesktop ) ;

    // Execute process in new desktop
    StartProcess( szDesktopName, szPath ) ;

    // Restore original ...
    SwitchDesktop( hOriginalInput ) ;
    SetThreadDesktop( hOriginalThread ) ;

    // Close the Desktop
    CloseDesktop( hNewDesktop ) ;

    return 0 ;
}

BOOL StartProcess( char *szDesktopName, char *szPath )
{
    STARTUPINFO         si ;
    PROCESS_INFORMATION pi ;

    // Zero these structs
    ZeroMemory( &si, sizeof( si ) ) ;
    si.cb = sizeof( si ) ;
     si.lpTitle = szDesktopName ;
     si.lpDesktop = szDesktopName ;
    ZeroMemory( &pi, sizeof( pi ) ) ;

    // Start the child process
    if (!CreateProcess( NULL,    // No module name (use command line).
                        szPath,  // Command line.
                        NULL,    // Process handle not inheritable.
                        NULL,    // Thread handle not inheritable.
                        FALSE,   // Set handle inheritance to FALSE.
                        0,       // No creation flags.
                        NULL,    // Use parent's environment block.
                        NULL,    // Use parent's starting directory.
                        &si,     // Pointer to STARTUPINFO structure.
                        &pi ) )  // Pointer to PROCESS_INFORMATION structure.
    {
        return FALSE ;
    }

    // Wait until process exits
    WaitForSingleObject( pi.hProcess, INFINITE ) ;

    // Close process and thread handles
    CloseHandle( pi.hProcess ) ;
    CloseHandle( pi.hThread ) ;

    return TRUE ;
}

int WINAPI Clock_Show_Hide( BOOL bShowHide )
{
    HWND    hWnd ;

    hWnd = GetDlgItem( FindWindow( TASKBAR, NULL ), ID_TRAY ) ;
     hWnd = GetDlgItem( hWnd, ID_CLOCK ) ;
    if ( hWnd == NULL )
        return 0 ;

    ShowWindow( hWnd, bShowHide ? SW_SHOW : SW_HIDE ) ;
    UpdateWindow( hWnd ) ;

    return 1 ;
}

int WINAPI AltTab_Enable_Disable( HWND hWnd, BOOL bEnableDisable )
{
    #define m_nHotKeyID 100

    if ( ! bEnableDisable )
    {
        if ( ! RegisterHotKey( hWnd, m_nHotKeyID+0, MOD_ALT, VK_TAB ) )     // Alt+Tab
            return 0 ;
      if ( ! RegisterHotKey( hWnd, m_nHotKeyID+1, MOD_ALT, VK_ESCAPE ) )    // Alt+Esc
            return 0 ;
    }
    else
    {
        if ( ! UnregisterHotKey( hWnd, m_nHotKeyID + 0 ) )
            return 0 ;
        if ( ! UnregisterHotKey( hWnd, m_nHotKeyID + 1 ) )
            return 0 ;
    }
    return 1 ;
}
#pragma ENDDUMP

/*
Saludos

Manuel Mercado
*/
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341

Continue the discussion