Switch scrolling direction with shortkey - scroll

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

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)
...
}

Simulate mouse left bottom and right bottom

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.

AutoHotKey: Toggle RAlt to Mouse Hotkey with Another Hotkey

I'm trying to create a Hotkey (Win+Shift+Q) that toggles on/off another Hotkey that changes the right Alt key to a left mouse click; however, I can't get it to work.
Expected Behavior:
Pressing Windows+Shift+Q will initially toggle the Right-Alt key to act as a left mouse click.
Pressing Windows+Shift+Q again will toggle the Right-Alt key back to acting as a Right-Alt key.
Pressing Windows+Shift+Q again will revert to the left-click behavior (see #1). And so on.
Here's the most current iteration of my code:
Hotkey, RAlt, MyClick, On
#+Q:: ;Win+Shift+Q :: ::Right-Alt acts as a left mouse button click
switch := !switch
MsgBox %switch%
Hotkey RAlt, % (switch ? "Off": "On")
Return
MyClick:
MouseClick
Return
When I run my script I get the following error after clicking OK on the MsgBox and the script quits:
Error: Nonexistent hotkey
Specifically: RAlt
Line#
141: Hotkey,RAlt,MyClick,On
143: switch:=!switch
144: MsgBox %switch%
-->145: Hotkey RAlt, % (switch ? "Off": "On")
146: Return
149: MouseClick
150: Return
The current thread will exit.
Most of the other posts that might relate (Can AutoHotKey toggle keymapping?, Autohotkey: Toggle a set of keybinds on and off) only deal with key to key mapping and not key to mouse mapping. I can't tell if that is the cause of my issues or not.
Previously I had this, but the Win+Shift+Q didn't toggle the behavior, RAlt always acted as a left-click so I commented it out:
#+Q:: ;Win+Shift+Q :: ::Right-Alt acts as a left mouse button click
RAlt::LButton
;Hotkey, RAlt, Toggle ;Does not work for some reason
int += 1
test := mod(int, 2) = 0
if (test) {
msgbox on
Hotkey, RAlt, On
}
else {
msgbox off
Hotkey, leftClick, Off
}
Return
I'll also add that I would like this behavior across Windows, not just a single application (which also seems to be a topic in other posts that allows for the #IfWinActive-type suggestions/solutions).
I tried your current iteration of code in AutoHotkey v1.1.13.01 Unicode 32-bit and I don't have any errors after pressing OK on the message box, the script works as advertised.
Try updating your AutoHotkey version here: http://ahkscript.org/download/ and see if the problem persists.
bState:=False
#If bState
RAlt::Click
#If
#+vk51:: ; win + shift + q
KeyWait, vk51
TrayTip, % "state of switch", % (bState:=!bState) ? "on":"off"
Return

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

How can I tell if a menu is open in VB6?

I've got a timer set up to detect if the mouse is over a certain area on my form, which you can imagine to be a rectangle starting at 50,50 (pixels) and ending at 1000,500. If the mouse is inside that rectangle, a second window pops up that acts somewhat like a tooltip, following the mouse around. The problem is that the menus at the top drape over this rectangle, and if you try to use a menu, the second window pops up (the timer sets its visible property to true) as soon as you move down the menu, which ends up closing the menu (I guess due to a loss of focus or something.)
If I can detect when one of the menus is open, I can disable the showing of the tooltip window with an if statement, but I don't know how to do that.
I think I've figured out how to do this by searching WIN32API.txt for "menu" and a little bit of googling, but I'm not really sure. Perhaps this solution only works on my machine.
Putting this code...
Dim hMenu As Long
hMenu = GetMenu(Form1.hwnd)
MsgBox GetMenuState(hMenu, 0, MF_BYPOSITION)
on a timer with an interval of 5000 allows you to the view the menu's state. In a closed state, the number appears to be random (1552, 1296, etc.,) but when the menu is opened, it is offset by 128 from this base value. A menu whose state is 1552 when closed is 1680 when open.
I'm not sure why it's offset by 128 or if this works on all machines (just to be safe, I will program it to check for inequality, not offset by 128), but it appears to be working for me.
If there is a problem with this solution or if there is a better way, please respond with another answer, and I'll be glad to give you credit for the answer instead.
Written by SBEIH Iyad - Syria - Damascus. 4/1/2021.
Using VB6.0, we can.
We check all menus windows if one is opened, it means: menu is opened.
we use API "GetMenu" for hWnd of main-menu,
then with the API "GetMenuState" we check if one of menus is opened.
Vb6.0 CODE:
Private Function GetBit_I(ByVal X As Long, ByVal i As Integer) As Integer
' Get the bit number i of X.
GetBit_I = (X And (2 ^ i)) / (2 ^ i)
End Function
Private Function MainMenuIsOpened(FRM As Form) As Boolean
Const MF_BYPOSITION = 1024
Dim H As Long, i As Integer, L As Long, MCount As Long
MainMenuIsOpened = False
On Error GoTo MainMenuIsOpenedError
H = GetMenu(FRM.HWnd)
MCount = GetMenuItemCount(H)
' MCount is the number of main-menus.
Do While (i < MCount)
L = GetMenuState(H, i, MF_BYPOSITION)
If ((L > -1) And (GetBit_I(L, 7) = 1)) Then
MainMenuIsOpened = True
Exit Do
End If
i = i + 1
Loop
Exit Function
MainMenuIsOpenedError:
MainMenuIsOpened = False
End Function
Good luck.

Resources