Simulate mouse left bottom and right bottom - winapi

I want to simulate mouse movement, but not just one way. I need my mouse to go to the bottom left, then go to the bottom right, then finally go back to the bottom left again.
bottom left, 2 seconds
bottom right, 2 seconds
bottom left again, 2 seconds
But I can't make it work. What do I need to do? Is this possible?
If we can not do all three; we can do first 2. i.e. only bottom left 2 sec then bottom right 2 sec.
I have this in ahk but its only one way, always going to the left bottom. I need it in C++.
#NoEnv
SendMode Input
~F6::Suspend
~End::ExitApp
~F5::Reload
LCtrl & ~LButton::
Loop
If GetKeyState("LButton", "LCtrl") {
Sleep, 3
moveAmount := (moveAmount = 0.2) ? 3 : -1
mouseXY(moveAmount,2)
}
else
break
~LButton::
Loop
If GetKeyState("LButton") {
Sleep, 3
moveAmount := (moveAmount = 0.2) ? 3 : -1
mouseXY(moveAmount,2)
}
else
break
Return
mouseXY(x,y)
{
DllCall("mouse_event",int,1,int,x,int,y,uint,0,uint,0)
}

You should be using GetAsyncKeyState, not GetKeyState. GetKeyState returns the value captured when the last message was dispatched.
Also, check out SendInput on MSDN. mouse_event has long been deprecated.
And please, add tag 'winapi' to your question.

Related

GetKeyBoardState() always shows some keys were pressed even though they were not after randomly pressing keyboard for a while

I am currently learning Go and trying to make a game which allows players to press several buttons at the same time. Right now, my code works for a while, but sometimes when I randomly press, the GetKeyboardState() always records one or more keys as pressed. The weird key only can be released when I press the key again. Then everything goes correctly again.
For example, I randomly press Up, Down, Left, Right, and right shift bottom on the keyboard to control the character in the game. Suddenly, the character always tries to go right and if I press Up or Down arrow, the character will just go to the top right or the bottom right. Only if I press the right arrow button again, the character can stop go right.
I thought maybe the game goes too fast so I limit the fps under 90 but the situation still exists.
Also, I tried to print out the keyboard status and it always shows the weird bottom was pressed when the situation happened even if I did not touch my keyboard.
func GetInput(keyState []uint8, prevKeyState []uint8) []Input {
input := make([]Input, 0)
if keyState[sdl.SCANCODE_UP] != 0 {
input = append(input, Input{Up})
}
...
// Handle Down, Right, Left
if keyState[sdl.SCANCODE_RSHIFT] == 0 && prevKeyState[sdl.SCANCODE_RSHIFT] != 0 {
input = append(input, Input{RShift})
}
if keyState[sdl.SCANCODE_B] == 0 && prevKeyState[sdl.SCANCODE_B] != 0
{
input = append(input, Input{Debug})
}
// something like prevKeyState = keystate
return input
}
How I use it
keyState := sdl.GetKeyboardState()
prevKeyState := make([]uint8, len(keyState))
for {
...
input := ui.GetInput(keyState, prevKeyState)
HandleInput(input, board)
...
}

Visual Basic: Increasing integer every 3 clicks

So my question is pretty much what the title says: how do I get a certain integer to increase by 1 for every three clicks of a button? For example, let's say the integer is 900. I click button1 once, then again, then again, and on the third click the integer changes to 901. How can this be accomplished?
Thanks in advance.
Use a counter that is actually three times larger behind the scene. Start at 2700 instead of 900, and increase the value by one for each click.
When you want to display the value that increases every third click, you divide the counter by three:
displayValue = counter \ 3
Try this maybe this could help or give you idea
Dim click = 0
Dim num = 900
if (click % 3 = 0)
num+=1
else
click+=1
end if

Fix multiple middle mouse click

Basically I have an issue where the middle mouse button when clicked does multiple very fast middle mouses. For example, if I open a link in a new tab with middle mouse it will open about 10 of that tab. I have tried all of the conventional methods to fix it, ie. driver fixes etc. What I want to try now is a bit of mouse debouncing with AHK (Auto Hot Key) for windows.
Essentially what I am thinking is to do this:
while (forever)
if( capture the middle mouse)
sleep 500 ms
mouse click
end
end
Can anyone give some advice with this approach?
Alternatively i thought about making a middle mouse hotkey:
$MButton::
Loop
{
sleep 500
if not GetKeyState("MButton", "P")
break ; Break out of the loop.
}
send {MButton}
return
Can anyone see any problems with this?
You can have a much simpler solution without a delay.
This will ignore middle click if the last click was 50 ms ago.
#Persistent
global pressed_g := 0
global delay_g := 50 ; delay in miliseconds, increase this value if your multiple click take longer than delay_g time
return
MButton::
if( pressed_g = 0 )
{
Send, {MButton}
tooltip,sent
pressed_g := 1
}
SetTimer, Countdown , Off
SetTimer, Countdown , -%delay_g%
return
Countdown:
pressed_g := 0
return
Could it be that you are looking for this? You press the MButton and while you keep the MButton pressed, the script will continue to fire MButton.
#Persistent
MButton::
while GetKeyState("MButton", "P") ; While the Middle Mouse button key is being held down
{
Send, {MButton}
}
return

Switch scrolling direction with shortkey

I currently have an AHK script which switches the scrolling direction of my mouse.
WheelUp::
Send {WheelDown}
Return
WheelDown::
Send {WheelUp}
Return
My colleagues don't like this and use my computer sometimes.
How can I assign a shortkey to switch the scrolling direction?
What I want:
When I press win+z the scrolling direction is changed, when I pres win+z again, the scrolling direction is changed back.
So basically the scrolling direction can be changed when pressing win+z
Is that possible with AHK?
Yes you can modify your hotkeys to have more code.
You will have to use if statements and variables.
Example:
global direction := 1
^s:: ; ctrl + s will launch this code you can modify this to win + z
direction := Mod( direction + 1 , 2 ) ; alternates values of direction between 1 and 0
return
WheelUp::
if(direction)
Send {WheelDown}
else
Send {WheelUp}
Return
; and reverse for wheeldown

How do I converge to and fro, between two numbers?

I'm trying to animate a man running:
Frames 1 to 5 = man leaning into run.
Frames 6 to 15 = man running a single step
frame = 1
frame +=1 //frames progress forwards at rate of 1 frame
function run(){
if(frame>15){ //at frame 15: man has completed leaning into run and completed one 'running' cycle
frame -=2 //frames now start to go backwards at rate of (1-2=)-1 frame
if(frame<6){ //until they reach frame 6 (beginning of running animation)
frame +=2 //frames start to progress forwards again at rate of (2-2+1=)+1 frame again
My method is really bad and seems to be only capable of going forwards then backwards ONCE between 15 and 6.
Does anyone know how I can bounce between these two numbers indefinitely?
After reaching frame = 15 and beginning your trip downward, you hit a condition (14) where neither of your IF statements is true. So your frame neither increments nor decrements. Stuck.
A possible better solution would be to maintain a variable called myDirection which toggles periodically between 1 and -1. That is, set myDirection = -1 when you hit 15, and set myDirection = 1 when you hit 6. Then, your iterative statement could always say frame = frame + myDirection and it would always be doing something -- you'd never be stuck doing nothing.
Ok, so using LesterDove's + schnaader's useful tips I've managed:
int step=1
function run(){
frame += step
if(frame>15){ step = -1}
if(frame<6){ step = 1}
}
and it works great guys. Thanks again!

Resources