dcsimg
Results 1 to 2 of 2

Thread: vb.net GetAsyncKeyState problem

  1. #1

    Thread Starter
    Lively Member manbearpig001's Avatar
    Join Date
    Oct 2009
    Posts
    73

    vb.net GetAsyncKeyState problem

    Hey guys! im trying to make a program that only listens to a few keys that the user presses, and if one of them is pressed, it carries out a specified command.

    for example, right now, i found a keylogger that could carry out my command, but id rather use something else if possible.

    right now, i have a timer running this:

    Code:
     For i = 1 To 255
                result = 0
                result = GetAsyncKeyState(i)
    
                If result = -32767 Then
                    TextBox1.Text = TextBox1.Text + Chr(i)
                End If
            Next i
    Then, in the textbox1_textchanged, i search for like if textbox1.text.contains("M") or something.

    problem with this, is im pretty sure its a keylogger, and i dont want to have to have that. also, it does not work in vista. Anyone have any thoughts or ideas on how i can fix this problem?

    Found a solution! (should have researched more throughly!)

    Option Strict On
    Imports System.Runtime.InteropServices

    Public Class Keyhook

    Private Const WH_KEYBOARD_LL As Integer = 13
    Private Const WM_KEYDOWN As Integer = &H100
    Private Shared _proc As LowLevelKeyboardProc = AddressOf HookCallback
    Private Shared _hookID As IntPtr = IntPtr.Zero

    Public Declare Auto Function SetWindowsHookEx Lib "user32.dll" ( _
    ByVal idHook As Integer, ByVal lpfn As LowLevelKeyboardProc, _
    ByVal hMod As IntPtr, ByVal dwThreadId As UInteger) As IntPtr

    Public Declare Auto Function UnhookWindowsHookEx _
    Lib "user32.dll" (ByVal hhk As IntPtr) As IntPtr

    Public Declare Auto Function CallNextHookEx _
    Lib "user32.dll" (ByVal hhk As IntPtr, ByVal nCode As Integer, _
    ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr

    Public Declare Auto Function GetModuleHandle Lib "kernel32.dll" ( _
    ByVal lpModuleName As String) As IntPtr


    Private Shared Function SetHook( _
    ByVal proc As LowLevelKeyboardProc) As IntPtr

    Dim curProcess As Process = Process.GetCurrentProcess()
    Dim curModule As ProcessModule = curProcess.MainModule

    Return SetWindowsHookEx(WH_KEYBOARD_LL, proc, _
    GetModuleHandle(curModule.ModuleName), 0)

    End Function

    Public Delegate Function LowLevelKeyboardProc( _
    ByVal nCode As Integer, ByVal wParam As IntPtr, _
    ByVal lParam As IntPtr) As IntPtr
    Public Shared Function HookCallback( _
    ByVal nCode As Integer, _
    ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr

    If nCode >= 0 And wParam = CType(WM_KEYDOWN, IntPtr) Then
    Dim vkCode As Integer = Marshal.ReadInt32(lParam)

    Keyhook.TextBox1.Text = (CType(vkCode, Keys).ToString)

    End If
    Return CallNextHookEx(_hookID, nCode, wParam, lParam)
    End Function

    Private Sub Keyhook_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    _hookID = SetHook(_proc)
    End Sub

    Private Sub Keyhook_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
    UnhookWindowsHookEx(_hookID)
    End Sub

    End Class
    Last edited by manbearpig001; Dec 15th, 2009 at 11:03 PM. Reason: found answer!

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: vb.net GetAsyncKeyState problem

    This thread uses the RegisterHotKey API. Give it a shot as it registers itself and is only fired when the hotkey is pressed.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width