Últimos mensajes - Powered by IBM
- Como saber el usuario logueado en otra PC desde ...
- Formato dinámico con string.format()
- Exportar contenido DataGridView a Excel
- Ignorar horas no laborales
- Problemas con Búsquedas por carácter especifico!
- Suma valores de columna en GridView
- Problema al copiar y pegar texto desde PDF a Word
- cadena de conexión para base de datos en red
- Curso iniciación a Videojuegos utilizando ...
- Crystal Report XI - Visual Studio 2010
Palabras claves
migracion a C#, heeeeeeeeeeeeelp...........
13/10/2004 - 17:20 por Nathaly | Informe spam
Hola a todostengo un inconveniente que me tiene de cabeza (ya algunos
dias ).. y quisiera saber si alguno de ustedes me puede ayudar
Estoy tratando de migrar una pequeña pantalla con 3 botones hecha en vb6 a
C# ya que necesito implementar en un sistema la funcionalidad de esa
pantalla(deshabilitar el Ctrl+Esc, Alt+Tab y Alt+Esc en win2000 y XP) .. sin
embargo al ejecutar mis programas migrados(en vb.net y c# -que creo estan
bien migrados- ) no ejecutan la funcionalidad que necesito a pesar que en
vb6 lo hace bien...
De dado varias vueltas al problema pero la verdad no se me ocurre que puede
ser de verdad si alguien me puede ayudar a resolver esto le estaria muy
agradecida
De antemano gracias a todos...
Les envio el codigo en vb6 y en C# y vb.net a ver si me pueden ayudar
checando este codigo..
//Codigo en C#;
//Formulario
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace manejoHook2
{
public class frmpantalla : System.Windows.Forms.Form
{
private System.Windows.Forms.Button cmdhook;
private System.Windows.Forms.Button cmdUnHook;
private System.Windows.Forms.Button cmdsalir;
private System.ComponentModel.Container components = null;
private Win32Imports objwin;
public frmpantalla()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after
InitializeComponent call
//
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.cmdhook = new System.Windows.Forms.Button();
this.cmdUnHook = new System.Windows.Forms.Button();
this.cmdsalir = new System.Windows.Forms.Button();
this.SuspendLayout();
this.cmdhook.Location = new System.Drawing.Point(104, 48);
this.cmdhook.Name = "cmdhook";
this.cmdhook.TabIndex = 0;
this.cmdhook.Text = "hook";
this.cmdhook.Click += new
System.EventHandler(this.cmdhook_Click);
//
// cmdUnHook
//
this.cmdUnHook.Location = new System.Drawing.Point(104,
96);
this.cmdUnHook.Name = "cmdUnHook";
this.cmdUnHook.TabIndex = 1;
this.cmdUnHook.Text = "unhook";
this.cmdUnHook.Click += new
System.EventHandler(this.cmdUnHook_Click);
//
// cmdsalir
//
this.cmdsalir.Location = new System.Drawing.Point(104,
144);
this.cmdsalir.Name = "cmdsalir";
this.cmdsalir.TabIndex = 2;
this.cmdsalir.Text = "Salir";
this.cmdsalir.Click += new
System.EventHandler(this.cmdsalir_Click);
//
// frmpantalla
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.cmdsalir);
this.Controls.Add(this.cmdUnHook);
this.Controls.Add(this.cmdhook);
this.Name = "frmpantalla";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new frmpantalla());
}
private void cmdhook_Click(object sender, System.EventArgs e)
{
objwin=new Win32Imports();
objwin.HookKeyB(System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]).ToInt64());
}
private void cmdUnHook_Click(object sender, System.EventArgs e)
{
objwin=new Win32Imports();
objwin.UnHookKeyB();
}
private void cmdsalir_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
//clase
using System;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace manejoHook2
{
public class Win32Imports
{
private long mHook;
private const long WH_KEYBOARD_LL = 13 ;
private const long HC_ACTION = 0;
// este es para el ratón
//Private Const WH_MOUSE_LL As Long = 14&
[StructLayout(LayoutKind.Sequential)]
private struct tagKBDLLHOOKSTRUCT
{
public long vkCode ;
public long scanCode;
public long flags ;
public long time ;
public long dwExtraInfo;
}
private const long VK_TAB = 0x09;
private const long VK_CONTROL = 0x11; // tecla Ctrl
//private const long VK_MENU = 0x12; // tecla Alt
private const long VK_ESCAPE = 0x1B;
//private const long VK_DELETE = 0x2E; // tecla Supr (Del)
private const long LLKHF_ALTDOWN = 0x20;
// '--
// ' Funciones del API de Windows
// '--
[System.Runtime.InteropServices.DllImportAttribute("user32",SetLastError=true
)]
private static extern long SetWindowsHookEx (long idHook , HookProc lpfn
,long hMod, long dwThreadId ) ;
[System.Runtime.InteropServices.DllImport("user32")]
private static extern long UnhookWindowsHookEx (long hHook) ;
[System.Runtime.InteropServices.DllImportAttribute("user32",SetLastError=true
)]
private static extern long CallNextHookEx (long hHook ,long nCode,long
wParam ,long lParam );
[System.Runtime.InteropServices.DllImportAttribute("user32",SetLastError=true
)]
private static extern short GetAsyncKeyState(long vKey );
[System.Runtime.InteropServices.DllImportAttribute("user32",SetLastError=true
)]
private static extern void CopyMemory ( tagKBDLLHOOKSTRUCT Destination ,
long Source ,long Length );
public Win32Imports()
{
}
private delegate long HookProc(long nCode,long wParam ,long lParam);
//delegado
private HookProc mycallback;
// La función a usar para el gancho del teclado
public long LLKeyBoardProc(long nCode,long wParam ,long lParam )
{
tagKBDLLHOOKSTRUCT pkbhs=new tagKBDLLHOOKSTRUCT();
long ret;
ret = 0;
bool bControlKeyDown = false;
int intaux;
CopyMemory
(pkbhs,lParam,System.Runtime.InteropServices.Marshal.SizeOf(pkbhs));
if (nCode == HC_ACTION)
{intaux=GetAsyncKeyState (VK_CONTROL) >>
((System.Runtime.InteropServices.Marshal.SizeOf(typeof(short)) * 8) - 1);
bControlKeyDown = System.Boolean.Parse(intaux.ToString());
// si se pulsa Ctrl+Esc
if ((pkbhs.vkCode == VK_ESCAPE)&& (bControlKeyDown))
{
ret = 1;
}
// si se pulsa Alt+Tab
if (pkbhs.vkCode == VK_TAB)
{
if ((pkbhs.flags ^ LLKHF_ALTDOWN) != 0 )
ret = 1;
}
// si se pulsa Alt+Esc
if (pkbhs.vkCode == VK_ESCAPE)
{
if ((pkbhs.flags ^ LLKHF_ALTDOWN) != 0)
ret = 1;
}
}
if (ret == 0)
ret = CallNextHookEx(mHook, nCode, wParam, lParam);
return ret;
}
public void HookKeyB(long hMod )
{
mycallback=new HookProc(LLKeyBoardProc);
mHook = SetWindowsHookEx(WH_KEYBOARD_LL,mycallback , hMod,
0);
int
intvalor=System.Runtime.InteropServices.Marshal.GetLastWin32Error();
System.Windows.Forms.MessageBox.Show(intvalor.ToString());
}
public void UnHookKeyB()
{
if (mHook != 0)
UnhookWindowsHookEx (mHook);
}
}
}
'Codigo en VB6:
' Codigo del Formulario
Option Explicit
Private Sub cmdCerrar_Click()
Unload Me
End Sub
Private Sub cmdHook_Click()
' iniciar el gancho para el teclado
HookKeyB App.hInstance
End Sub
Private Sub cmdUnHook_Click()
' quitar el gancho del teclado
UnHookKeyB
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
' al cerrar el formulario, quitar el gancho del teclado
UnHookKeyB
End Sub
' Código del módulo BAS:
Option Explicit
' para guardar el gancho creado con SetWindowsHookEx
Private mHook As Long
' para indicar a SetWindowsHookEx que tipo de gancho queremos instalar
Private Const WH_KEYBOARD_LL As Long = 13&
Private Type tagKBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type
'
Private Const VK_TAB As Long = &H9
Private Const VK_CONTROL As Long = &H11 ' tecla Ctrl
'Private Const VK_MENU As Long = &H12 ' tecla Alt
Private Const VK_ESCAPE As Long = &H1B
'Private Const VK_DELETE As Long = &H2E ' tecla Supr (Del)
'
Private Const LLKHF_ALTDOWN As Long = &H20&
' códigos para los ganchos (la acción a tomar en el gancho del teclado)
Private Const HC_ACTION As Long = 0&
'--
' Funciones del API de Windows
'--
' para asignar un gancho (hook)
Private Declare Function SetWindowsHookEx Lib "user32" Alias
"SetWindowsHookExA" _
(ByVal idHook As Long, ByVal lpfn As Long, _
ByVal hMod As Long, ByVal dwThreadId As Long) As Long
' para quitar el gancho creado con SetWindowsHookEx
Private Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long
' para llamar al siguiente gancho
Private Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Long, ByVal nCode As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
' para saber si se ha pulsado en una tecla
Private Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer
' para copiar la estructura en un long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, Source As Any, ByVal Length As Long)
' La función a usar para el gancho del teclado
Public Function LLKeyBoardProc(ByVal nCode As Long, _
ByVal wParam As Long, _
ByVal lParam As Long _
) As Long
Dim pkbhs As tagKBDLLHOOKSTRUCT
Dim ret As Long
ret = 0
' copiar el parámetro en la estructura
CopyMemory pkbhs, ByVal lParam, Len(pkbhs)
If nCode = HC_ACTION Then
' si se pulsa Ctrl+Esc
If pkbhs.vkCode = VK_ESCAPE Then
If (GetAsyncKeyState(VK_CONTROL) And &H8000) 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
End If
If ret = 0 Then
ret = CallNextHookEx(mHook, nCode, wParam, lParam)
End If
LLKeyBoardProc = ret
' El código C++ en el que se ha basado (o casi) el de VB
'LRESULT CALLBACK LowLevelKeyboardProc (INT nCode, WPARAM wParam, LPARAM
lParam)
'{
' // By returning a non-zero value from the hook procedure, the
' // message does not get passed to the target window
' KBDLLHOOKSTRUCT *pkbhs = (KBDLLHOOKSTRUCT *) lParam;
' BOOL bControlKeyDown = 0;
'
' switch (nCode)
' {
' case HC_ACTION:
' {
' // Check to see if the CTRL key is pressed
' bControlKeyDown = GetAsyncKeyState (VK_CONTROL) >>
((sizeof(SHORT) * 8) - 1);
'
' // Disable CTRL+ESC
' if (pkbhs->vkCode == VK_ESCAPE && bControlKeyDown)
' return 1;
'
' // Disable ALT+TAB
' if (pkbhs->vkCode == VK_TAB && pkbhs->flags & LLKHF_ALTDOWN)
' return 1;
'
' // Disable ALT+ESC
' if (pkbhs->vkCode == VK_ESCAPE && pkbhs->flags & LLKHF_ALTDOWN)
' return 1;
'
' break;
' }
'
'default:
' break;
' } return CallNextHookEx (hHook, nCode, wParam, lParam);
'}
End Function
Public Sub HookKeyB(ByVal hMod As Long)
' instalar el gancho para el teclado
' hMod será el valor de App.hInstance de la aplicación
mHook = SetWindowsHookEx(WH_KEYBOARD_LL, 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
Vb.net
Eventos del Formulario:
Private Sub cdmhook_Click(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles cdmhook.Click
HookKeyB(System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32)
End Sub
Private Sub cdmsalir_Click(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles cdmsalir.Click
Me.Close()
End Sub
Private Sub cmdUnhook_Click(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles cmdUnhook.Click
UnHookKeyB()
End Sub
Private Sub Form1_Closing(ByVal eventSender As System.Object, ByVal
eventArgs As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Dim Cancel As Short = eventArgs.Cancel
UnHookKeyB()
eventArgs.Cancel = Cancel
End Sub
Modulo:
Option Strict Off
Option Explicit On
Module Modulo
Private mHook As Integer
Private Const WH_KEYBOARD_LL As Integer = 13
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 = &H9s
Private Const VK_CONTROL As Integer = &H11s ' tecla Ctrl
Private Const VK_ESCAPE As Integer = &H1Bs
Private Const LLKHF_ALTDOWN As Integer = &H20
Private Const HC_ACTION As Integer = 0
Private Declare Function SetWindowsHookEx Lib "user32" Alias
"SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As LLKeyBoardProc2,
ByVal hMod As Integer, ByVal dwThreadId As Integer) As Integer
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook
As Integer) As Integer
Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As
Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As
Integer) As Integer
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As
Integer) As Short
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory"
(ByRef Destination As tagKBDLLHOOKSTRUCT, ByRef Source As Integer, ByVal
Length As Integer)
Public Delegate Function LLKeyBoardProc2(ByVal nCode As Integer, ByVal
wParam As Integer, ByVal lParam As Integer) As Integer
Public miDelegadoInstanciado As LLKeyBoardProc2
Public Function LLKeyBoardProc(ByVal nCode As Integer, ByVal wParam As
Integer, ByVal lParam As Integer) As Integer
Dim pkbhs As tagKBDLLHOOKSTRUCT
Dim ret As Integer
ret = 0
CopyMemory(pkbhs, lParam, Len(pkbhs))
If nCode = HC_ACTION Then
' si se pulsa Ctrl+Esc
If pkbhs.vkCode = VK_ESCAPE Then
If (GetAsyncKeyState(VK_CONTROL) And &H8000s) 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
'
End If
'
If ret = 0 Then
ret = CallNextHookEx(mHook, nCode, wParam, lParam)
End If
'
LLKeyBoardProc = ret
End Function
Public Sub HookKeyB(ByVal hMod As Integer)
mHook = SetWindowsHookEx(WH_KEYBOARD_LL, miDelegadoInstanciado,
hMod, 0)
End Sub
Public Sub UnHookKeyB()
If mHook <> 0 Then
UnhookWindowsHookEx(mHook)
End If
End Sub
Public Sub Main()
miDelegadoInstanciado = New LLKeyBoardProc2(AddressOf
LLKeyBoardProc)
Application.Run(New Form1)
End Sub
End Module
dias ).. y quisiera saber si alguno de ustedes me puede ayudar
Estoy tratando de migrar una pequeña pantalla con 3 botones hecha en vb6 a
C# ya que necesito implementar en un sistema la funcionalidad de esa
pantalla(deshabilitar el Ctrl+Esc, Alt+Tab y Alt+Esc en win2000 y XP) .. sin
embargo al ejecutar mis programas migrados(en vb.net y c# -que creo estan
bien migrados- ) no ejecutan la funcionalidad que necesito a pesar que en
vb6 lo hace bien...
De dado varias vueltas al problema pero la verdad no se me ocurre que puede
ser de verdad si alguien me puede ayudar a resolver esto le estaria muy
agradecida
De antemano gracias a todos...
Les envio el codigo en vb6 y en C# y vb.net a ver si me pueden ayudar
checando este codigo..
//Codigo en C#;
//Formulario
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace manejoHook2
{
public class frmpantalla : System.Windows.Forms.Form
{
private System.Windows.Forms.Button cmdhook;
private System.Windows.Forms.Button cmdUnHook;
private System.Windows.Forms.Button cmdsalir;
private System.ComponentModel.Container components = null;
private Win32Imports objwin;
public frmpantalla()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after
InitializeComponent call
//
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.cmdhook = new System.Windows.Forms.Button();
this.cmdUnHook = new System.Windows.Forms.Button();
this.cmdsalir = new System.Windows.Forms.Button();
this.SuspendLayout();
this.cmdhook.Location = new System.Drawing.Point(104, 48);
this.cmdhook.Name = "cmdhook";
this.cmdhook.TabIndex = 0;
this.cmdhook.Text = "hook";
this.cmdhook.Click += new
System.EventHandler(this.cmdhook_Click);
//
// cmdUnHook
//
this.cmdUnHook.Location = new System.Drawing.Point(104,
96);
this.cmdUnHook.Name = "cmdUnHook";
this.cmdUnHook.TabIndex = 1;
this.cmdUnHook.Text = "unhook";
this.cmdUnHook.Click += new
System.EventHandler(this.cmdUnHook_Click);
//
// cmdsalir
//
this.cmdsalir.Location = new System.Drawing.Point(104,
144);
this.cmdsalir.Name = "cmdsalir";
this.cmdsalir.TabIndex = 2;
this.cmdsalir.Text = "Salir";
this.cmdsalir.Click += new
System.EventHandler(this.cmdsalir_Click);
//
// frmpantalla
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.cmdsalir);
this.Controls.Add(this.cmdUnHook);
this.Controls.Add(this.cmdhook);
this.Name = "frmpantalla";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new frmpantalla());
}
private void cmdhook_Click(object sender, System.EventArgs e)
{
objwin=new Win32Imports();
objwin.HookKeyB(System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]).ToInt64());
}
private void cmdUnHook_Click(object sender, System.EventArgs e)
{
objwin=new Win32Imports();
objwin.UnHookKeyB();
}
private void cmdsalir_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
//clase
using System;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace manejoHook2
{
public class Win32Imports
{
private long mHook;
private const long WH_KEYBOARD_LL = 13 ;
private const long HC_ACTION = 0;
// este es para el ratón
//Private Const WH_MOUSE_LL As Long = 14&
[StructLayout(LayoutKind.Sequential)]
private struct tagKBDLLHOOKSTRUCT
{
public long vkCode ;
public long scanCode;
public long flags ;
public long time ;
public long dwExtraInfo;
}
private const long VK_TAB = 0x09;
private const long VK_CONTROL = 0x11; // tecla Ctrl
//private const long VK_MENU = 0x12; // tecla Alt
private const long VK_ESCAPE = 0x1B;
//private const long VK_DELETE = 0x2E; // tecla Supr (Del)
private const long LLKHF_ALTDOWN = 0x20;
// '--
// ' Funciones del API de Windows
// '--
[System.Runtime.InteropServices.DllImportAttribute("user32",SetLastError=true
)]
private static extern long SetWindowsHookEx (long idHook , HookProc lpfn
,long hMod, long dwThreadId ) ;
[System.Runtime.InteropServices.DllImport("user32")]
private static extern long UnhookWindowsHookEx (long hHook) ;
[System.Runtime.InteropServices.DllImportAttribute("user32",SetLastError=true
)]
private static extern long CallNextHookEx (long hHook ,long nCode,long
wParam ,long lParam );
[System.Runtime.InteropServices.DllImportAttribute("user32",SetLastError=true
)]
private static extern short GetAsyncKeyState(long vKey );
[System.Runtime.InteropServices.DllImportAttribute("user32",SetLastError=true
)]
private static extern void CopyMemory ( tagKBDLLHOOKSTRUCT Destination ,
long Source ,long Length );
public Win32Imports()
{
}
private delegate long HookProc(long nCode,long wParam ,long lParam);
//delegado
private HookProc mycallback;
// La función a usar para el gancho del teclado
public long LLKeyBoardProc(long nCode,long wParam ,long lParam )
{
tagKBDLLHOOKSTRUCT pkbhs=new tagKBDLLHOOKSTRUCT();
long ret;
ret = 0;
bool bControlKeyDown = false;
int intaux;
CopyMemory
(pkbhs,lParam,System.Runtime.InteropServices.Marshal.SizeOf(pkbhs));
if (nCode == HC_ACTION)
{intaux=GetAsyncKeyState (VK_CONTROL) >>
((System.Runtime.InteropServices.Marshal.SizeOf(typeof(short)) * 8) - 1);
bControlKeyDown = System.Boolean.Parse(intaux.ToString());
// si se pulsa Ctrl+Esc
if ((pkbhs.vkCode == VK_ESCAPE)&& (bControlKeyDown))
{
ret = 1;
}
// si se pulsa Alt+Tab
if (pkbhs.vkCode == VK_TAB)
{
if ((pkbhs.flags ^ LLKHF_ALTDOWN) != 0 )
ret = 1;
}
// si se pulsa Alt+Esc
if (pkbhs.vkCode == VK_ESCAPE)
{
if ((pkbhs.flags ^ LLKHF_ALTDOWN) != 0)
ret = 1;
}
}
if (ret == 0)
ret = CallNextHookEx(mHook, nCode, wParam, lParam);
return ret;
}
public void HookKeyB(long hMod )
{
mycallback=new HookProc(LLKeyBoardProc);
mHook = SetWindowsHookEx(WH_KEYBOARD_LL,mycallback , hMod,
0);
int
intvalor=System.Runtime.InteropServices.Marshal.GetLastWin32Error();
System.Windows.Forms.MessageBox.Show(intvalor.ToString());
}
public void UnHookKeyB()
{
if (mHook != 0)
UnhookWindowsHookEx (mHook);
}
}
}
'Codigo en VB6:
' Codigo del Formulario
Option Explicit
Private Sub cmdCerrar_Click()
Unload Me
End Sub
Private Sub cmdHook_Click()
' iniciar el gancho para el teclado
HookKeyB App.hInstance
End Sub
Private Sub cmdUnHook_Click()
' quitar el gancho del teclado
UnHookKeyB
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
' al cerrar el formulario, quitar el gancho del teclado
UnHookKeyB
End Sub
' Código del módulo BAS:
Option Explicit
' para guardar el gancho creado con SetWindowsHookEx
Private mHook As Long
' para indicar a SetWindowsHookEx que tipo de gancho queremos instalar
Private Const WH_KEYBOARD_LL As Long = 13&
Private Type tagKBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type
'
Private Const VK_TAB As Long = &H9
Private Const VK_CONTROL As Long = &H11 ' tecla Ctrl
'Private Const VK_MENU As Long = &H12 ' tecla Alt
Private Const VK_ESCAPE As Long = &H1B
'Private Const VK_DELETE As Long = &H2E ' tecla Supr (Del)
'
Private Const LLKHF_ALTDOWN As Long = &H20&
' códigos para los ganchos (la acción a tomar en el gancho del teclado)
Private Const HC_ACTION As Long = 0&
'--
' Funciones del API de Windows
'--
' para asignar un gancho (hook)
Private Declare Function SetWindowsHookEx Lib "user32" Alias
"SetWindowsHookExA" _
(ByVal idHook As Long, ByVal lpfn As Long, _
ByVal hMod As Long, ByVal dwThreadId As Long) As Long
' para quitar el gancho creado con SetWindowsHookEx
Private Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long
' para llamar al siguiente gancho
Private Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Long, ByVal nCode As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
' para saber si se ha pulsado en una tecla
Private Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer
' para copiar la estructura en un long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, Source As Any, ByVal Length As Long)
' La función a usar para el gancho del teclado
Public Function LLKeyBoardProc(ByVal nCode As Long, _
ByVal wParam As Long, _
ByVal lParam As Long _
) As Long
Dim pkbhs As tagKBDLLHOOKSTRUCT
Dim ret As Long
ret = 0
' copiar el parámetro en la estructura
CopyMemory pkbhs, ByVal lParam, Len(pkbhs)
If nCode = HC_ACTION Then
' si se pulsa Ctrl+Esc
If pkbhs.vkCode = VK_ESCAPE Then
If (GetAsyncKeyState(VK_CONTROL) And &H8000) 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
End If
If ret = 0 Then
ret = CallNextHookEx(mHook, nCode, wParam, lParam)
End If
LLKeyBoardProc = ret
' El código C++ en el que se ha basado (o casi) el de VB
'LRESULT CALLBACK LowLevelKeyboardProc (INT nCode, WPARAM wParam, LPARAM
lParam)
'{
' // By returning a non-zero value from the hook procedure, the
' // message does not get passed to the target window
' KBDLLHOOKSTRUCT *pkbhs = (KBDLLHOOKSTRUCT *) lParam;
' BOOL bControlKeyDown = 0;
'
' switch (nCode)
' {
' case HC_ACTION:
' {
' // Check to see if the CTRL key is pressed
' bControlKeyDown = GetAsyncKeyState (VK_CONTROL) >>
((sizeof(SHORT) * 8) - 1);
'
' // Disable CTRL+ESC
' if (pkbhs->vkCode == VK_ESCAPE && bControlKeyDown)
' return 1;
'
' // Disable ALT+TAB
' if (pkbhs->vkCode == VK_TAB && pkbhs->flags & LLKHF_ALTDOWN)
' return 1;
'
' // Disable ALT+ESC
' if (pkbhs->vkCode == VK_ESCAPE && pkbhs->flags & LLKHF_ALTDOWN)
' return 1;
'
' break;
' }
'
'default:
' break;
' } return CallNextHookEx (hHook, nCode, wParam, lParam);
'}
End Function
Public Sub HookKeyB(ByVal hMod As Long)
' instalar el gancho para el teclado
' hMod será el valor de App.hInstance de la aplicación
mHook = SetWindowsHookEx(WH_KEYBOARD_LL, 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
Vb.net
Eventos del Formulario:
Private Sub cdmhook_Click(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles cdmhook.Click
HookKeyB(System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32)
End Sub
Private Sub cdmsalir_Click(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles cdmsalir.Click
Me.Close()
End Sub
Private Sub cmdUnhook_Click(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles cmdUnhook.Click
UnHookKeyB()
End Sub
Private Sub Form1_Closing(ByVal eventSender As System.Object, ByVal
eventArgs As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Dim Cancel As Short = eventArgs.Cancel
UnHookKeyB()
eventArgs.Cancel = Cancel
End Sub
Modulo:
Option Strict Off
Option Explicit On
Module Modulo
Private mHook As Integer
Private Const WH_KEYBOARD_LL As Integer = 13
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 = &H9s
Private Const VK_CONTROL As Integer = &H11s ' tecla Ctrl
Private Const VK_ESCAPE As Integer = &H1Bs
Private Const LLKHF_ALTDOWN As Integer = &H20
Private Const HC_ACTION As Integer = 0
Private Declare Function SetWindowsHookEx Lib "user32" Alias
"SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As LLKeyBoardProc2,
ByVal hMod As Integer, ByVal dwThreadId As Integer) As Integer
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook
As Integer) As Integer
Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As
Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As
Integer) As Integer
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As
Integer) As Short
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory"
(ByRef Destination As tagKBDLLHOOKSTRUCT, ByRef Source As Integer, ByVal
Length As Integer)
Public Delegate Function LLKeyBoardProc2(ByVal nCode As Integer, ByVal
wParam As Integer, ByVal lParam As Integer) As Integer
Public miDelegadoInstanciado As LLKeyBoardProc2
Public Function LLKeyBoardProc(ByVal nCode As Integer, ByVal wParam As
Integer, ByVal lParam As Integer) As Integer
Dim pkbhs As tagKBDLLHOOKSTRUCT
Dim ret As Integer
ret = 0
CopyMemory(pkbhs, lParam, Len(pkbhs))
If nCode = HC_ACTION Then
' si se pulsa Ctrl+Esc
If pkbhs.vkCode = VK_ESCAPE Then
If (GetAsyncKeyState(VK_CONTROL) And &H8000s) 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
'
End If
'
If ret = 0 Then
ret = CallNextHookEx(mHook, nCode, wParam, lParam)
End If
'
LLKeyBoardProc = ret
End Function
Public Sub HookKeyB(ByVal hMod As Integer)
mHook = SetWindowsHookEx(WH_KEYBOARD_LL, miDelegadoInstanciado,
hMod, 0)
End Sub
Public Sub UnHookKeyB()
If mHook <> 0 Then
UnhookWindowsHookEx(mHook)
End If
End Sub
Public Sub Main()
miDelegadoInstanciado = New LLKeyBoardProc2(AddressOf
LLKeyBoardProc)
Application.Run(New Form1)
End Sub
End Module
Leer las respuestas