Results 1 to 10 of 10

Thread: [2005] how do i use "GetAsyncKeyState" API.

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2008
    Posts
    5

    [2005] how do i use "GetAsyncKeyState" API.

    hi all,

    i am facing problem in using this API "GetAsyncKeyState" can any body please give some coding or some important hints regarding this problem, how could i use it, because i did all possible search but unable to find.

    please help me............


    regards

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] how do i use "GetAsyncKeyState" API.

    have a look at my keycodes utility in my signature.

    heres the declaration for the api

    vb Code:
    1. Public Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Integer) As Integer

    to use it

    vb Code:
    1. if GetAsyncKeyState(keycode) then
    2.    'key is depressed
    3. end if

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2008
    Posts
    5

    Re: [2005] how do i use "GetAsyncKeyState" API.

    In keycode there i will give the code of key as like 32 to 120, but i require that it should give me a keypressed value in generating message.

    thanks

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] how do i use "GetAsyncKeyState" API.

    have a look at the hotkeys link in my signature

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2008
    Posts
    5

    Re: [2005] how do i use "GetAsyncKeyState" API.

    my dear, i am agian disturbing you because, problem is not solved , i am going to use "GetAsyncKeyState" API to generate a messagebox to show keypressed value, when i press any key from keyboard.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] how do i use "GetAsyncKeyState" API.

    GetAsyncKeyState doesn't notify you when a key is pressed. you call GetAsyncKeyState to check if a key is being held down.

    try this

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
    4.         MsgBox(e.KeyChar)
    5.     End Sub
    6.  
    7.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.         Me.KeyPreview = True
    9.     End Sub
    10.  
    11.  
    12. End Class

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] how do i use "GetAsyncKeyState" API.

    is this for a keylogger?

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2008
    Posts
    5

    Re: [2005] how do i use "GetAsyncKeyState" API.

    thanks for taking some time to answer my question but, the coding that i am sending you it belongs to vb 6.0, it is all functioning well and giving accurate result as i require,but i am unable to convert it in vb.NET 2005

    if you have some time then i will request you how to convert this code in vb.NET 2005

    Code:
    //In module
    
    Public Const DT_CENTER = &H1
    Public Const DT_WORDBREAK = &H10
    Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    Declare Function DrawTextEx Lib "user32" Alias "DrawTextExA" (ByVal hDC As Long, ByVal lpsz As String, ByVal n As Long, lpRect As RECT, ByVal un As Long, ByVal lpDrawTextParams As Any) As Long
    Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
    Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    Global Cnt As Long, sSave As String, sOld As String, Ret As String
    Dim Tel As Long
    Function GetPressedKey() As String
        For Cnt = 32 To 128
            'Get the keystate of a specified key
            If GetAsyncKeyState(Cnt) <> 0 Then
                GetPressedKey = Chr$(Cnt)
                Exit For
            End If
        Next Cnt
    End Function
    Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
        Ret = GetPressedKey
        If Ret <> sOld Then
            sOld = Ret
            sSave = sSave + sOld
        End If
    End Sub
    
    //In Form1
    Private Sub Form_Load()
            Me.Caption = "action form"
            SetTimer Me.hwnd, 0, 1, AddressOf TimerProc
    End Sub
    Private Sub Form_Paint()
        Dim R As RECT
        Const mStr = "Start from here."
        'Clear the form
        Me.Cls
        'API uses pixels
        Me.ScaleMode = vbPixels
        'Set the rectangle's values
        SetRect R, 0, 0, Me.ScaleWidth, Me.ScaleHeight
        'Draw the text on the form
        DrawTextEx Me.hDC, mStr, Len(mStr), R, DT_WORDBREAK Or DT_CENTER, ByVal 0&
    End Sub
    Private Sub Form_Resize()
        Form_Paint
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
        'Kill our API-timer
        KillTimer Me.hwnd, 0
        'Show all the typed keys
        MsgBox sSave
    End Sub

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] how do i use "GetAsyncKeyState" API.

    thats a keylogger. its against the policy of this forum to assist with malicious software

  10. #10

    Thread Starter
    New Member
    Join Date
    May 2008
    Posts
    5

    Re: [2005] how do i use "GetAsyncKeyState" API.

    my post #8 contains "GetAsyncKeyState" API, so how to use it in vb.NET

    best wished for you in completion of my work

Posting Permissions

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



Click Here to Expand Forum to Full Width