Results 1 to 27 of 27

Thread: Keyboard + mouse

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    399

    Keyboard + mouse

    Hey, I want to make a program so everytime I hit 'q' on my keyboard it left clicks my mouse.. I want it to work exactly as if I hit the mouse button myself.. is this possible?

  2. #2
    Addicted Member
    Join Date
    Feb 2006
    Location
    Hyderabad, India
    Posts
    233

    Re: Keyboard + mouse

    You will need a keyboard hook for that. Following api's might help you to start off :
    setwindowshookex
    unhookwindowshookex
    keyboardproc
    The forum has threads on this particular issue. Search for it.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    399

    Re: Keyboard + mouse

    Ty very much

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    399

    Re: Keyboard + mouse

    How would I do the mouse click part? i dont want to use


    Public Const MOUSEEVENTF_LEFTDOWN = &H2
    Public Const MOUSEEVENTF_LEFTUP = &H4

    cause for some reason they arent working on one application, they work every where else on the screen, but not on that application

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Keyboard + mouse

    You could use sendkeys to send vbKeyLButton to your application. Or do you want to click the button in some other application?

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    399

    Re: Keyboard + mouse

    Yes I want to have it click on other applications

  7. #7
    Addicted Member
    Join Date
    Mar 2006
    Posts
    187

    Re: Keyboard + mouse

    in a new form with this code you can move mouse with keyboard

    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
    Private Type POINTAPI
    x As Long
    y As Long
    End Type
    Dim myPos As POINTAPI

    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    GetCursorPos myPos
    Select Case KeyCode
    Case 37, vbKeyA: myPos.x = myPos.x - 10 'Left key or A
    Case 38, vbKeyW: myPos.y = myPos.y - 10 'Top key or W
    Case 39, vbKeyD: myPos.x = myPos.x + 10 'Right Key or D
    Case 40, vbKeyS: myPos.y = myPos.y + 10 'Down key or S
    End Select
    SetCursorPos myPos.x, myPos.y
    End Sub




    for click part i 'm learning and so I don't know....
    i hope to explain you....eheh hi!

  8. #8
    Addicted Member
    Join Date
    Feb 2006
    Location
    Hyderabad, India
    Posts
    233

    Re: Keyboard + mouse

    this might help you.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    399

    Re: Keyboard + mouse

    srisa, that could is useful but it sends the click to a handle, I dont want to do that, I want to just send the click whereever the cursor is

  10. #10
    Addicted Member
    Join Date
    Feb 2006
    Location
    Hyderabad, India
    Posts
    233

    Re: Keyboard + mouse

    In post 6 you said you want to send the click event to another application. If that is the case you have to get a handle to the window of that application and send the message.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    399

    Re: Keyboard + mouse

    I want to send the click to a button on the application but none of the buttons have handles, the application only has 1 handle, its written in java or something, so I need to move the cursor there and click the button.. moving the cursor to the button is easy.. clicking it under normal circumstances would be easy, however this application somehow catches virtual mouse clicks and ignores them.. so I need a way simulate a mouse click that tricks the program and makes it think its a real click

  12. #12
    Addicted Member
    Join Date
    Feb 2006
    Location
    Hyderabad, India
    Posts
    233

    Re: Keyboard + mouse

    So , what you want to do is to programatically click a command button in an application. With my limited knowledge , I think that is different from mouse_click event.

  13. #13
    Addicted Member
    Join Date
    Feb 2006
    Location
    Hyderabad, India
    Posts
    233

    Re: Keyboard + mouse

    That being the case, you need to get the handle of the application using findwindow api. After getting the handle of the application window, you can get the handle to the button using findwindowex api, which gets the handle of the child window .(command button is considered to be a child window)

    VB Code:
    1. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    2. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    3.  
    4.    tWnd = FindWindow("Shell_TrayWnd", vbNullString)
    5.     'Get the start-button's window handle
    6.     bWnd = FindWindowEx(tWnd, ByVal 0&, "BUTTON", vbNullString)
    this code is from api guide.
    After getting handle to the button you can use sendmessage api.
    this might be of use.

  14. #14
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Keyboard + mouse

    Quote Originally Posted by bail3yz
    I want to send the click to a button on the application but none of the buttons have handles, the application only has 1 handle, its written in java or something, so I need to move the cursor there and click the button.
    If it's running in Windows, the button has a handle (it's an object that was drawn on the screen).

    You have to get the handle of the application whose window is under the cursor, then you have to get the handle of that button in that application, then you send the button a mouse left (or right) click.

    You can't just send the application a click unless the application is subclassing all clicks and knows that a click means to execute that button's code - or the application checks the actual location of the cursor relative to itself before processing clicks. Do you do that in your click event? I don't.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    399

    Re: Keyboard + mouse

    No, the button does not have to have a handle.. for example a flash application buttons dont have handles, nor does a Java application... which is why I wanted to move the cursor to the location and click..

    Something like this

    Public Function ClickCoords(X As Long, Y As Long)
    SetCursorPos X, Y
    mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
    mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
    mouse_event MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0
    mouse_event MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0
    End Function

    Should work.. it works on every application, including mine and I dont check the location of the cursor before processing clicks..

  16. #16
    Addicted Member
    Join Date
    Feb 2006
    Location
    Hyderabad, India
    Posts
    233

    Re: Keyboard + mouse

    How about this : Set the cursor position and then use sendmessage with
    HWND_BROADCAST as the parameter for the window handle. Then all the top_level windows will receive the message.

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    399

    Re: Keyboard + mouse

    I'll give that a shot, thank you.. Ill let you know if it works

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    399

    Re: Keyboard + mouse

    nope, no luck there.. once again that method worked on every window except this one application.. i swear they are capturing the simulated mouse clicks somehow and blocking them

  19. #19
    Addicted Member
    Join Date
    Feb 2006
    Location
    Hyderabad, India
    Posts
    233

    Re: Keyboard + mouse

    I am sorry, but I don't know much. Most of what I have posted is done by searching through google. That is one option left. Or if one of the experienced guys around here has a look at your post, they might help you out.

  20. #20
    Addicted Member
    Join Date
    Feb 2006
    Location
    Hyderabad, India
    Posts
    233

    Re: Keyboard + mouse

    this link uses sendinput. See if that helps.

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    399

    Re: Keyboard + mouse

    ya I tried that too lol no luck , thanks for all the help tho srisa

  22. #22
    Lively Member
    Join Date
    Jul 2005
    Posts
    73

    Re: Keyboard + mouse

    use AutoIT

    and heres the code (should work)

    When you use this you wont be able to use your mouse till you pause the script with w

    VB Code:
    1. HotKeySet("q","Click")
    2. HotKeySet("w","Waiting")
    3.  
    4. Call("Waiting")
    5.  
    6. Func Waiting()
    7. while 1 = 1
    8. sleep(1000)
    9. wend
    10. EndFunc
    11.  
    12. Func Click()
    13. $Pos = MouseGetPos()
    14. while 1 = 1
    15.   MouseClick("left",$Pos[0], $Pos[1], 1, 0)
    16. wend
    17. EndFunc
    Last edited by BladeZ; Apr 10th, 2006 at 08:53 PM.

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    399

    Re: Keyboard + mouse

    i will give that a shot, but if autoit could do it, shouldnt there be away to code it into a VB app?

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    399

    Re: Keyboard + mouse

    that actually worked Bladez wish I knew how to do that in VB without 3rd party software..

  25. #25

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    399

    Re: Keyboard + mouse

    i see autoit has a DLL version, is there anyway to load that into vb and get it to work via my VB app?

  26. #26
    Lively Member
    Join Date
    Jul 2005
    Posts
    73

    Re: Keyboard + mouse

    Heres a thread i found that has a example of how to do it

    Thread

  27. #27

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    399

    Re: Keyboard + mouse

    dude, you are my hero ty so much

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