Post: Visual Basic Program Minimized Help
11-23-2011, 12:21 AM #1
(adsbygoogle = window.adsbygoogle || []).push({}); So i made this AutoClicker
You must login or register to view this content.

When you are using the program (On top)
It works. But when you minimize the program the hotkeys don't work. How can i make this program so when it is
minimized or in the system tray it will work.

Aka when i press F1 it will start clicking

Here is my code.

    Public Class Autoclicker
Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwflags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cbuttons As Long, ByVal dwExtraInfo As Long)


Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.F2 Then
Timer1.Start()
Else
If e.KeyCode = Keys.F3 Then
Timer1.Stop()

End If
End If

End Sub


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
KeyPreview = True
AutoSizeMode = False



End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Start()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Timer1.Stop()

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
TextBox1.Text = TextBox1.Text + 1



End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Windows.Forms.Cursor.Position = New System.Drawing.Point(Windows.Forms.Cursor.Position)
mouse_event(&H2, 0, 0, 0, 1)
mouse_event(&H4, 0, 0, 0, 1)
End Sub



Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
System.Diagnostics.Process.Start("mailto:" & "[email protected]")
End Sub

End Class




Take a look and help me out. Thanks
11-23-2011, 12:37 AM #2
Pichu
RIP PICHU.
Originally posted by xThaSlothx View Post
So i made this AutoClicker
You must login or register to view this content.

When you are using the program (On top)
It works. But when you minimize the program the hotkeys don't work. How can i make this program so when it is
minimized or in the system tray it will work.

Aka when i press F1 it will start clicking

Here is my code.

    Public Class Autoclicker
Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwflags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cbuttons As Long, ByVal dwExtraInfo As Long)


Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.F2 Then
Timer1.Start()
Else
If e.KeyCode = Keys.F3 Then
Timer1.Stop()

End If
End If

End Sub


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
KeyPreview = True
AutoSizeMode = False



End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Start()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Timer1.Stop()

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
TextBox1.Text = TextBox1.Text + 1



End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Windows.Forms.Cursor.Position = New System.Drawing.Point(Windows.Forms.Cursor.Position)
mouse_event(&H2, 0, 0, 0, 1)
mouse_event(&H4, 0, 0, 0, 1)
End Sub



Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
System.Diagnostics.Process.Start("mailto:" & "[email protected]")
End Sub

End Class




Take a look and help me out. Thanks


Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.F2 Then
Timer1.Start()
Else
If e.KeyCode = Keys.F3 Then
Timer1.Stop()


End If
End If


End Sub

You have the keys be toggled when the form is active and the event Keydown is used. For a quick fix, setup a timer and enable it and set value to 10. Then have it check to see if the key is pressed and if so then give the result.
11-23-2011, 12:49 AM #3
Originally posted by Sublimity View Post
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.F2 Then
Timer1.Start()
Else
If e.KeyCode = Keys.F3 Then
Timer1.Stop()


End If
End If


End Sub

You have the keys be toggled when the form is active and the event Keydown is used. For a quick fix, setup a timer and enable it and set value to 10. Then have it check to see if the key is pressed and if so then give the result.




I don't quite understand. stare

---------- Post added at 07:49 PM ---------- Previous post was at 07:47 PM ----------

I am already using a timer for the buttons and how fast it clicks.
You must login or register to view this content.

And heres the properties
You must login or register to view this content.




I have my buttons on like you know
timer1.start/stop

and my timer is on:
    
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Windows.Forms.Cursor.Position = New System.Drawing.Point(Windows.Forms.Cursor.Position)
mouse_event(&H2, 0, 0, 0, 1)
mouse_event(&H4, 0, 0, 0, 1)
End Sub




So i wasn't sure with what you suggested
Last edited by Agent~47 ; 11-23-2011 at 12:58 AM.
11-23-2011, 12:55 AM #4
Pichu
RIP PICHU.
Originally posted by xThaSlothx View Post
I don't quite understand. stare

---------- Post added at 07:49 PM ---------- Previous post was at 07:47 PM ----------

I am already using a timer for the buttons and how fast it clicks.
You must login or register to view this content.

And heres the properties
You must login or register to view this content.


What I am saying is this, you have it setup where the event is handled while the form is open, I see that you toggle on or off the timer by buttons.

You can setup a timer that will check to see if F1 or F3 is pressed. If F1 is pressed then timer 1 starts if F3 is pressed Timer 1 stops. Just like that, although the HotKeytimer will always remain active.

The timer then can work with the form minimized.

Also, you may want to add to the code where when the timer is started that the form is minimized and if the timer stops that it is brought back to original state.
11-23-2011, 01:03 AM #5
Originally posted by Sublimity View Post
What I am saying is this, you have it setup where the event is handled while the form is open, I see that you toggle on or off the timer by buttons.

You can setup a timer that will check to see if F1 or F3 is pressed. If F1 is pressed then timer 1 starts if F3 is pressed Timer 1 stops. Just like that, although the HotKeytimer will always remain active.

The timer then can work with the form minimized.

Also, you may want to add to the code where when the timer is started that the form is minimized and if the timer stops that it is brought back to original state.



Thanks for this i will do this now


Edit:I have no clue how to do this though :dumb:
Last edited by Agent~47 ; 11-23-2011 at 01:05 AM.
11-23-2011, 01:05 AM #6
Pichu
RIP PICHU.
Originally posted by xThaSlothx View Post
Thanks for this i will do this now


No problem, when you are done let me know, I'd like to see the finished product. Smile
11-23-2011, 01:16 AM #7
Originally posted by Sublimity View Post
No problem, when you are done let me know, I'd like to see the finished product. Smile


As i mentioned im not sure how to do this, so i gave it a shot.

Here is my second timer
    
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
If e.KeyCode = Keys.F2 Then
Timer1.Start()
Else
If e.keycode = Keys.F3 Then
Timer1.Stop()

End If
End If


But then i get You must login or register to view this content.
11-23-2011, 06:33 PM #8
Epic?
Awe-Inspiring
Originally posted by xThaSlothx View Post
As i mentioned im not sure how to do this, so i gave it a shot.

Here is my second timer
    
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
If e.KeyCode = Keys.F2 Then
Timer1.Start()
Else
If e.keycode = Keys.F3 Then
Timer1.Stop()

End If
End If


But then i get You must login or register to view this content.



A few things you ought to know...

The problem you're experiencing is resulting from the fact that when the window is minimized, it "looses focus", which is probably why the hotkeys stopped working (because the hotkeys aren't global, and they only were affecting the presently opened window, and not the form, which was minimized).

The reason why your newest code isn't working is because of the Sub procedure head:
    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick



You're then trying to use e.KeyCode, but take a look at where e is declared: ByVal e As System.EventArgs.

e does not have access to KeyCode because KeyCode is not a member of EventArgs, KeyCode is a member of System.Windows.Forms.KeyEventArgs.


As far as fixes go, there are a couple possibilities for hotkeys on Windows. I would recommend You must login or register to view this content., its safe and easy to use. Here's a little demonstration for you:
    
<System.Runtime.InteropServices.DllImport("user32.dll")> _
Private Declare Function GetAsyncKeyState(ByVal vkey As System.Windows.Forms.Keys) As Integer
End Function

Private Sub myTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles myTimer.Tick
Dim hotkey As Boolean = GetAsyncKeyState(Keys.F2)
If hotkey = True Then
' do something
End If
End Sub


Note that you'll have to edit the above code. First off, older versions of Visual Basic .NET will not support that method for importing the DLL. Furthermore, you'll have to change myTimer to the name of your timer (especially where it says Handles myTimer.Tick, but it's better style to change the name of the Sub procedure as well).

You'll also need to change Keys.F2 in the call to GetAsyncKeyState to the correct hotkey. Furthermore, you'll need to change the "do something" comment to the code you want to execute when the hotkey is pressed.

Lastly, if you want to add more hotkeys, you'll need to GetAsyncKeyState again (but for the new hotkey) and once again check if it's true or not.


Also, remember that there is more than one way (and more than one API) to create these "global hotkeys", GetAsyncKeyState is just one way, I recommended it because it is easy to implement and understand.


I hope this helps! Please let me know if you have any questions!
Last edited by Epic? ; 11-23-2011 at 07:27 PM.
11-23-2011, 08:16 PM #9
Originally posted by Epic
A few things you ought to know...

The problem you're experiencing is resulting from the fact that when the window is minimized, it "looses focus", which is probably why the hotkeys stopped working (because the hotkeys aren't global, and they only were affecting the presently opened window, and not the form, which was minimized).

The reason why your newest code isn't working is because of the Sub procedure head:
    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick



You're then trying to use e.KeyCode, but take a look at where e is declared: ByVal e As System.EventArgs.

e does not have access to KeyCode because KeyCode is not a member of EventArgs, KeyCode is a member of System.Windows.Forms.KeyEventArgs.


As far as fixes go, there are a couple possibilities for hotkeys on Windows. I would recommend You must login or register to view this content., its safe and easy to use. Here's a little demonstration for you:
    
<System.Runtime.InteropServices.DllImport("user32.dll")> _
Private Declare Function GetAsyncKeyState(ByVal vkey As System.Windows.Forms.Keys) As Integer
End Function

Private Sub myTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles myTimer.Tick
Dim hotkey As Boolean = GetAsyncKeyState(Keys.F2)
If hotkey = True Then
' do something
End If
End Sub


Note that you'll have to edit the above code. First off, older versions of Visual Basic .NET will not support that method for importing the DLL. Furthermore, you'll have to change myTimer to the name of your timer (especially where it says Handles myTimer.Tick, but it's better style to change the name of the Sub procedure as well).

You'll also need to change Keys.F2 in the call to GetAsyncKeyState to the correct hotkey. Furthermore, you'll need to change the "do something" comment to the code you want to execute when the hotkey is pressed.

Lastly, if you want to add more hotkeys, you'll need to GetAsyncKeyState again (but for the new hotkey) and once again check if it's true or not.


Also, remember that there is more than one way (and more than one API) to create these "global hotkeys", GetAsyncKeyState is just one way, I recommended it because it is easy to implement and understand.


I hope this helps! Please let me know if you have any questions!



Thank you i used this so i started editing and i am getting this now:
         <System.Runtime.InteropServices.DllImport("user32.dll")> _ 
Private Declare Function GetAsyncKeyState(ByVal vkey As System.Windows.Forms.Keys) As Integer
End Function

Private Sub myTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim hotkey As Boolean = GetAsyncKeyState(Keys.F2)
If hotkey = True Then
' do something
End If
End Sub
End Class


But now>
    Error	1	'System.Runtime.InteropServices.DllImportAttribute' cannot be applied to a Declare.
Error 2 'Lib' expected.
Error 3 'End Function' must be preceded by a matching 'Function'.


You must login or register to view this content.
You must login or register to view this content.
11-23-2011, 09:03 PM #10
Epic?
Awe-Inspiring
Originally posted by xThaSlothx View Post
Thank you i used this so i started editing and i am getting this now:
         <System.Runtime.InteropServices.DllImport("user32.dll")> _ 
Private Declare Function GetAsyncKeyState(ByVal vkey As System.Windows.Forms.Keys) As Integer
End Function

Private Sub myTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim hotkey As Boolean = GetAsyncKeyState(Keys.F2)
If hotkey = True Then
' do something
End If
End Sub
End Class


But now>
    Error	1	'System.Runtime.InteropServices.DllImportAttribute' cannot be applied to a Declare.
Error 2 'Lib' expected.
Error 3 'End Function' must be preceded by a matching 'Function'.


You must login or register to view this content.
You must login or register to view this content.


Like I stated in my previous post, the method I demonstrated for importing user32.dll only works on newer versions of Visual Basic .NET.

First, try removing the End Function from the GetAsyncKeyState declaration, see if that fixes it, if not, proceed to step two:

Second, try replacing the function declaration for GetAsyncKeyState with:
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer

Just replace the whole function (including the End Function).

If that doesn't work, let me know, and also tell me what version of the .NET framework you are using and what version of Visual Basic .NET (and the version of the studio, for example: 2008, 2010, etc.).

If you have any further questions, let me know!


EDIT: It may be helpful for you to read through the You must login or register to view this content. or try to follow a You must login or register to view this content., but again, if you have any questions, I'll still be happy to help.
Last edited by Epic? ; 11-23-2011 at 09:14 PM.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo