Trying to create a simple while loop using Rbutton VB.Net - vb.net

Not important:
I started learning VB.Net yesterday, I'm learning basic functions that I use in other languages, but I don't find much direct content about VB.Net
The real question:
I have no idea how i can use while RButton down loop something... ill send what i need to loop, if someone can help me...
Edit: I found another problem, i can send the keys in game chatbox, but the real function of the keys isnt working)
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
KEYB_Event(VK_KEY_Q, 0, 0, 0)
KEYB_Event(VK_KEY_Q, 0, 0, 0)
KEYB_Event(VK_KEY_Q, 0, KEYEVENTF_KEYUP, 0)
Thread.Sleep(5)
KEYB_Event(VK_KEY_W, 0, 0, 0)
KEYB_Event(VK_KEY_W, 0, 0, 0)
KEYB_Event(VK_KEY_W, 0, KEYEVENTF_KEYUP, 0)
Thread.Sleep(5)
KEYB_Event(VK_KEY_E, 0, 0, 0)
KEYB_Event(VK_KEY_E, 0, 0, 0)
KEYB_Event(VK_KEY_E, 0, KEYEVENTF_KEYUP, 0)
Thread.Sleep(5)
KEYB_Event(VK_KEY_E, 0, KEYEVENTF_KEYUP, 0)
End Sub

Related

VB.net Autoclick sound file play on top of each other

I've been trying for a really long time now to play an audio file that is like a click with a Timer that clicks at the same time as the mouse clicks as an autoclicker, but I haven't gotten anything to work...
Here is my code:
Private Sub Click_Tick(sender As Object, e As EventArgs) Handles Click.Tick
mouse_event(mouseclickdown, 0, 0, 0, 0)
Dim AudioFile = New MCIPlayer(Me, TextBox3.Text)
AudioFile.Play(AudioPlayMode.Background)
Thread.Sleep(20)
mouse_event(mouseclickup, 0, 0, 0, 0)
End Sub
I used MCI Player that I found from another post, that got the best result so far, but it still doesn't work like I want it to.
I am on Visual Studio 2022 "net6.0-windows" target framework.

Mouse event wheel occurs last

something strange is happening in my application and I can't figure out what it is. I am trying to simulate Mouse click and scroll programmatically.
I can successfully perform Mouse click and scroll programmatically, but the problem is that the mouse wheel occurs last.
This is the code:
<DllImport("user32.dll", EntryPoint:="mouse_event")>
Private Shared Sub mouse_event(ByVal dwFlags As UInteger, ByVal dx As
Integer, ByVal dy As Integer, ByVal dwData As Integer, ByVal dwExtraInfo
As UInteger)
End Sub
Flags:
Const MOUSEEVENTF_LEFTDOWN As UInteger = &H2 '0x0002'
Const MOUSEEVENTF_LEFTUP As UInteger = &H4 '0x0004'
Const MOUSEEVENTF_WHEEL As UInteger = &H800
Const MOUSEEVENTF_XDOWN As UInteger = &H80
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
scroll_to_end()
Thread.Sleep(300)
click_perf()
MessageBox.Show("S")
End Sub
Private Sub scroll_to_end()
'Focus panel1'
c_point = New Point(Panel1.Location.X + 1, Panel1.Location.Y + 1)
Cursor.Position = Me.PointToScreen(c_point)
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
'Wait'
Thread.Sleep(150)
'Scroll to the end of the page'
Dim scroll_down As Integer = -(10 * 120) 'Perform 10 scrolls down'
mouse_event(MOUSEEVENTF_WHEEL, 0, 0, scroll_down, 0)
'This is the fix:'
Application.DoEvents()
End Sub
Private Sub click_perf()
'Move mouse diagonally 200px'
c_point.X += 200
c_point.Y += 200
Cursor.Position = Me.PointToScreen(c_point)
'Perform click'
mouse_event(MOUSEEVENTF_LEFTDOWN, c_point.X, c_point.Y, 0, 0)
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
End Sub
When I click button3, I want to focus panel1 then scroll to the end and move mouse 200px diagonally downwards and then simulate click.
What actually happens: Mouse moves to panel1, simulates mouse button down and mouse button up. Then, it moves the mouse diagonally 200px and the messagebox shows up. The scroll didn't even occur and now when I close the messagebox panel1 probably loses the focus and doesn't scroll.
If I remove messagebox.show(""): Mouse moves to panel1 and focuses it just like above. It scrolls to the end, but it performs click before it was scrolled down.
EDIT:
To fix this issue we need to put Application.DoEvents() After WHEEL MOUSE EVENT.
Put this after each UI operation to force it to happen in order
Application.DoEvents()
UI events are handled in a message loop, which looks at a queue of messages and handles them. There is no guarantee that they will be handled in the order they are raised if they are raised around the same time in your code because your code is faster than the loop. Application.DoEvents() will halt your code execution and force the loop to handle all pending messages, thus making them happen in order.
It's generally considered poor practice to use this method, however, on account of its widespread misuse. I think your case for it is a good one and maybe the only I've ever seen.
I'd be interested to see if someone can provide a preferred alternative...

Global hotkey not registered in game using GetAsyncKeyState - vb.net

I've made a tool that will continuously click for the user if they decide to have it on with toggle keys etc, it all works fine in ordinary windows e.g google chrome, but when it comes to games it doesn't always work correctly.
(well it does in some games, then others it doesn't)
The code is designed to click fast while holding LButton, then stop when it's let go to act as an autoclicker (user has control of speed) which again works, but when in a game it clicks alot slower than it's suppose to / any other window / app.
I've figured out adding a delay using
Thread.Sleep(200)
fixes the speed of the autoclicker in game, but then it messes up the keybind which results in the autoclicker always clicking even when LButton isnt held / pressed.
Is there anything else that I could use as a delay, or anything else I can do to the code so it works correctly?
I've been trying many different variations and searching online the last few days trying to get it working, but none succeeded.
Here's all the code got to do with autoclicking in my project, i've added some notes to try and explain which part is doing what / speeds the timers are set to.
Imports System.Threading
Public Class Form1
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Integer) As Short
Private Declare Sub mouse_event Lib "user32" (ByVal dwflags As Integer, ByVal dx As Integer, ByVal cbuttons As Integer, ByVal dy As Integer, ByVal dwExtraInfo As Integer)
Private Const mouseclickup = 4
Private Const mouseclickdown = 2
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Timer1 isnt doing the clicking, Timer1 is just listening for LButton
'clicks which is why I have it always on aswell as a low interval.
Timer1.Start()
Timer1.Interval = 1
'LButton is the timer that will do the clicking.
LButton.Interval = 100
End Sub
Private Sub LButton_Tick(sender As Object, e As EventArgs) Handles LButton.Tick
If GetAsyncKeyState(Keys.LButton) Then
mouse_event(mouseclickup, 0, 0, 0, 0)
'Without Thread.Sleep(200) the code works as it's suppose to, clicks
'when LButton is held, stops clicking when LButton is let go,
'although without Thread.Sleep(200) it will not work in all games,
'but with it, it will continuously click even when LButton isn't held.
Thread.Sleep(200)
mouse_event(mouseclickdown, 0, 0, 0, 0)
Else
LButton.Stop()
End If
End Sub
Private Sub Timer1_Tick_1(sender As Object, e As EventArgs) Handles Timer1.Tick
'This is what will listen for the left clicks and also stop the left
'LButton timer if LButton is not held
If GetAsyncKeyState(Keys.LButton) Then
LButton.Start()
Else
LButton.Stop()
End If
End Sub
End Class
Quoting the MSDN documentation, GetAsyncKeyState() determines:
whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState.
So when you check the function via If GetAsyncKeyState(Keys.LButton) Then, it will return non-zero at least three times, thus execute the code more than you want (which is what you experience when you add Thread.Sleep(200)).
To check if the key is held down you have to check if the most significant bit is set, which for a Short is 0x8000 in hex and 32768 in decimal.
Checking a bit flag is done by checking (<number> And <bit>) = <bit> - where And is the bitwise And operator.
This would result in your code looking like this:
Const KeyDownBit As Integer = &H8000
Private Sub LButton_Tick(sender As Object, e As EventArgs) Handles LButton.Tick
If (GetAsyncKeyState(Keys.LButton) And KeyDownBit) = KeyDownBit Then
mouse_event(mouseclickup, 0, 0, 0, 0)
Thread.Sleep(200)
mouse_event(mouseclickdown, 0, 0, 0, 0)
Else
LButton.Stop()
End If
End Sub
Private Sub Timer1_Tick_1(sender As Object, e As EventArgs) Handles Timer1.Tick
If (GetAsyncKeyState(Keys.LButton) And KeyDownBit) = KeyDownBit Then
LButton.Start()
Else
LButton.Stop()
End If
End Sub
I'm not sure whether your second timer (Timer1) is actually needed in this case.

How to call public sub using button in VB.Net

In VB.Net, how can I call thePublic Sub using a button click?
Here's what I have so far:
Public Sub SetThreshold(ByVal e As PaintEventArgs)
' Open an Image file, and draw it to the screen.
Dim Image As New Bitmap(PictureBox1.Image)
PictureBox1.Image = Image
e.Graphics.DrawImage(Image, 20, 20)
' Create an ImageAttributes object, and set its color threshold.
Dim imageAttr As New ImageAttributes
imageAttr.SetThreshold(0.7F)
' Draw the image with the colors bifurcated.
Dim rect As New Rectangle(300, 20, 200, 200)
e.Graphics.DrawImage(Image, rect, 0, 0, 200, 200, GraphicsUnit.Pixel, imageAttr)
End Sub

VB.Net Photo Booth Project

So my older sister's high school graduation is coming up soon. She really enjoy's looking at pictures of her friend so I had this idea to make a photo booth style program. At this point, I have the webcam to display on a picturebox, however the click and mouseclick subroutines are not working. Does anyone know why this could be?
Private Sub picCapture_MouseClick(sender As Object, e As MouseEventArgs) Handles picCapture.MouseClick
MsgBox("hi")
End Sub
Dim iHeight As Integer = picCapture.Height
Dim iWidth As Integer = picCapture.Width
hHwnd = capCreateCaptureWindowA(DeviceID.ToString, WS_VISIBLE Or WS_CHILD, 0, 0, 1280, _
1024, picCapture.Handle.ToInt32, 0)
If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, DeviceID, 0) Then
SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)
SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)
'Start previewing the image from the camera
SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)
SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCapture.Width, picCapture.Height, _
SWP_NOMOVE Or SWP_NOZORDER)
Else
' Error connecting to device close window
DestroyWindow(hHwnd)
End If
Any help is appreciated. :)