Making the same key do different things.

Get help with using AutoHotkey and its commands and hotkeys
ExcelHotKeys
Posts: 20
Joined: 21 Dec 2020, 10:27

Making the same key do different things.

Post by ExcelHotKeys » 21 Jan 2021, 18:14

Is there a way to make wheelup do one thing in one program, and another in another program by specifying what program it is to be used with?

Basically I wanna combine two.

Code: Select all

xl := ComObjActive("excel.application")
+WheelUp::xl.ActiveSheet.Next.Select
+WheelDown::xl.ActiveSheet.Previous.Select
and this in microsoft edge.

Code: Select all

+WheelDown::Send ^{PgUp}
+WheelUp::Send ^{PgDn}
[Mod edit: [code][/code] tags added.]
ExcelHotKeys
Posts: 20
Joined: 21 Dec 2020, 10:27

Re: Making the same key do different things.

Post by ExcelHotKeys » 28 Jan 2021, 14:53

I tried. This is not working.

Code: Select all

#If WinActive MicrosoftEdge.exe
+WheelUp:: Send ^{PgUp}
+WheelDown::Send ^(PgDn}
Can someone write a code for me.....
User avatar
mikeyww
Posts: 5104
Joined: 09 Sep 2014, 18:38

Re: Making the same key do different things.

Post by mikeyww » 28 Jan 2021, 14:54

You're very close! Remove the space after #If.

Code: Select all

#IfWinActive ahk_exe MicrosoftEdge.exe
+WheelUp::Send ^{PgUp}
+WheelDown::Send ^{PgDn}
#IfWinActive
Here is info on creating a proper WinTitle.

Finally: a failed brace, corrected above.
ExcelHotKeys
Posts: 20
Joined: 21 Dec 2020, 10:27

Re: Making the same key do different things.

Post by ExcelHotKeys » 28 Jan 2021, 15:18

Code: Select all

#If WinActive "ahk_exe Microsoft Edge.exe"
+WheelUp::Send ^{PgDn}
+WheelDown::Send ^{PgUp}
Working now :)
ExcelHotKeys
Posts: 20
Joined: 21 Dec 2020, 10:27

Re: Making the same key do different things.

Post by ExcelHotKeys » 28 Jan 2021, 15:39

Is there a better way to rewrite this code?

Code: Select all

#If WinActive "ahk_exe Microsoft Edge.exe"
+WheelUp::Send ^{PgDn}
+WheelDown::Send ^{PgUp}
[Mod edit: [code][/code] tags added.]

When I move the wheel up or down, the action gets ignored sometimes.
User avatar
mikeyww
Posts: 5104
Joined: 09 Sep 2014, 18:38

Re: Making the same key do different things.

Post by mikeyww » 28 Jan 2021, 15:39

I think you have a flawed syntax, and it might not work as intended. Would use the format that I provided. Copy and paste my script.

Can you spot the difference with the spaces & quotation marks in the first line? It makes a world of difference! :)
ExcelHotKeys
Posts: 20
Joined: 21 Dec 2020, 10:27

Re: Making the same key do different things.

Post by ExcelHotKeys » 28 Jan 2021, 15:43

Your code doesn't work.
User avatar
mikeyww
Posts: 5104
Joined: 09 Sep 2014, 18:38

Re: Making the same key do different things.

Post by mikeyww » 28 Jan 2021, 15:44

I noticed that you probably have the wrong process name. My Edge is called something different, as follows.

ahk_exe msedge.exe

Are you seeing something different in Window Spy?

Would try this substitution.

The fix:

Code: Select all

#IfWinActive ahk_exe msedge.exe
+WheelUp::Send ^{PgUp}
+WheelDown::Send ^{PgDn}
#IfWinActive
Last edited by mikeyww on 28 Jan 2021, 15:47, edited 1 time in total.
ExcelHotKeys
Posts: 20
Joined: 21 Dec 2020, 10:27

Re: Making the same key do different things.

Post by ExcelHotKeys » 28 Jan 2021, 15:46

Code: Select all

xl := ComObjActive("excel.application")
+WheelUp::xl.ActiveSheet.Next.Select
+WheelDown::xl.ActiveSheet.Previous.Select

#If WinActive "ahk_exe Microsoft Edge.exe"
+WheelUp::Send ^{PgDn}
+WheelDown::Send ^{PgUp}
[Mod edit: [code][/code] tags added.]

Wheelup do cltr page up
Wheeldown do cltr page down
everytime.
Just want to use the scroll wheels to navigate the tabs more quickly.
User avatar
mikeyww
Posts: 5104
Joined: 09 Sep 2014, 18:38

Re: Making the same key do different things.

Post by mikeyww » 28 Jan 2021, 15:47

Try:

Code: Select all

#IfWinActive ahk_exe msedge.exe
+WheelUp::Send ^{PgUp}
+WheelDown::Send ^{PgDn}
#IfWinActive
This worked when I tested it in my Edge browser.
ExcelHotKeys
Posts: 20
Joined: 21 Dec 2020, 10:27

Re: Making the same key do different things.

Post by ExcelHotKeys » 28 Jan 2021, 15:54

Same problem when I tried to do the excel.
This solved it.

Code: Select all

xl := ComObjActive("excel.application")
+WheelUp::xl.ActiveSheet.Next.Select
+WheelDown::xl.ActiveSheet.Previous.Select
I just need to write something like this for edge.

This makes the code super consistent.
User avatar
mikeyww
Posts: 5104
Joined: 09 Sep 2014, 18:38

Re: Making the same key do different things.

Post by mikeyww » 28 Jan 2021, 16:07

Code: Select all

xl := ComObjActive("Excel.Application")

#IfWinActive ahk_exe EXCEL.exe
+WheelUp::xl.ActiveSheet.Next.Select
+WheelDown::xl.ActiveSheet.Previous.Select

#IfWinActive ahk_exe msedge.exe
+WheelUp::Send ^{PgUp}
+WheelDown::Send ^{PgDn}
#IfWinActive
This alone should work, though sorry if I am not understanding! Perhaps you actually want to use COM to change tabs? I tested this in both Excel & Edge.
ExcelHotKeys
Posts: 20
Joined: 21 Dec 2020, 10:27

Re: Making the same key do different things.

Post by ExcelHotKeys » 28 Jan 2021, 16:19

I want to write something like excel for edge because it is more consistent.
User avatar
mikeyww
Posts: 5104
Joined: 09 Sep 2014, 18:38

Re: Making the same key do different things.

Post by mikeyww » 28 Jan 2021, 16:21

OK. Sorry then; I misunderstood the problem. Please understand that you had the incorrect syntax initially, and I think that this led to the issue that you had.

Others: please chime in with the real answer!
ExcelHotKeys
Posts: 20
Joined: 21 Dec 2020, 10:27

Re: Making the same key do different things.

Post by ExcelHotKeys » 28 Jan 2021, 16:26

It's always been like that because simulating a key stroke will not work every time. This is the same issue I had with excel until someone replaced it with Com.
User avatar
mikeyww
Posts: 5104
Joined: 09 Sep 2014, 18:38

Re: Making the same key do different things.

Post by mikeyww » 28 Jan 2021, 16:42

OK. I can appreciate your point!
User avatar
submeg
Posts: 128
Joined: 14 Apr 2017, 20:39
GitHub: submeg

Re: Making the same key do different things.

Post by submeg » 30 Jan 2021, 08:02

In terms of making the same key do different things, I've used both the class of the program and also different "profiles" to help layer actions on the same key. Please see my Left and right mouse wheel scripts:

Code: Select all

;-------------------------------------------------------------

MouseAction_WheelLeft()
{
	
	WinGetTitle, titleIs, A
	SetTitleMatchMode, 2
	
	If WinActive("ahk_class SciTEWindow") {
		;go left in open scripts in SciTE
		Send, {LShift Down}{F6}{LShift Up}
		
	}
	
	Else If WinActive("ahk_class XLMAIN") {
		;Go left in worksheets in excel
		;Send, {Ctrl Down}{PgUp}{Ctrl Up}
		ComObjActive("Excel.Application").ActiveSheet.Previous.Activate
		
	}
	
	Else If WinActive("ahk_class Chrome_WidgetWin_1") {
		
		If  WinActive("Teams")
		{
			;toggle mute
			Send, ^+m
			
		}
		
		else
		{
			;Go back to previous tab in chrome
			Send, ^+{Tab}
			
		}
		
	}
	
	Else If WinActive("ahk_class MozillaWindowClass") {
		;Go left in tabs in Mozilla
		Send, {Ctrl Down}{PgUp}{Ctrl Up}
		
	}
	
	Else If WinActive("ahk_class rctrl_renwnd32") {
		;Go up in Outlook
		Send, {Up}
		
	}
	
	Else If WinActive("ahk_class AcrobatSDIWindow") {
		;go left in adobe acrobat
		Send, ^+{Tab}
		
	}   
	
    ;Spotify
	Else If WinActive("ahk_class Chrome_WidgetWin_0") {
		
		Send, {Ctrl down}
		Send, {Left}
		Send, {Ctrl up}
		
	}
	
	Else If WinActive("ahk_class ZPContentViewWndClass") {
		;Mute in Zoom
		Hotkey, !a, off
		Send, !a
		Hotkey, !a, on
	
	}
	
	Else If WinActive("ahk_class Framework::CFrame") {
	
		Send, ^+{Tab}
		
	}
	
	Else {
		
		;to do more than one case at a time use Case 2, 3:
		Switch MachineChoice
		{
			Case -1:	;BLANK
			
			
			Case 0:		;TEST
			
			
			Case 1:		;Work - Work PC
			
			switchDesktopToLeft()
			TrayTip, Desktop, Desktop %CurrentDesktop%.
			SetTimer, HideTrayTip, -2000
			
			
			Case 2:		;Work - Home PC
			
			switchDesktopToLeft()
			TrayTip, Desktop, Desktop %CurrentDesktop%.
			SetTimer, HideTrayTip, -2000
			
			Case 3:		;Work - Surface
			
			Send, {LCtrl down}{LWin down}
			Sleep, 5 
			Send, {Left}
			Send, {LWin up}{LCtrl up}
			
			TrayTip, Desktop, Desktop %CurrentDesktop%.
			SetTimer, HideTrayTip, -2000
			
			
			Case 4:		;Home - Home PC
			
			switchDesktopToLeft()
			TrayTip, Desktop, Desktop %CurrentDesktop%.
			SetTimer, HideTrayTip, -2000
			
			Case "4r":	;Home - Gaming (Rimworld)
			
			
			Case 5:		;Home - Surface
			
			Send, {LCtrl down}{LWin down}
			Sleep, 5 
			Send, {Left}
			Send, {LWin up}{LCtrl up}
			
			TrayTip, Desktop, Desktop %CurrentDesktop%.
			SetTimer, HideTrayTip, -2000
			
			
			Default:
			msgbox case error
		}
		
		
	}
	
}
Return 

;-------------------------------------------------------------

Code: Select all

;-------------------------------------------------------------

MouseAction_WheelRight()
{
	
	WinGetTitle, titleIs, A
	SetTitleMatchMode, 2
	
	If WinActive("ahk_class SciTEWindow") {
		
		Hotkey, F6, off
		
		Send, {F6}
		
		Hotkey, F6, on
		
	}
	
	Else If WinActive("ahk_class XLMAIN") {
		
		;Send, {Ctrl Down}{PgDn}{Ctrl Up}
		ComObjActive("Excel.Application").ActiveSheet.Next.Activate
		
	}
	
	Else If WinActive("ahk_class Chrome_WidgetWin_1") {
		
		If  WinActive("Teams")
		{
			;toggle camera
			Send, ^+o
			
		}
		
		else
		{
			;Go forward to next tab
			Send, ^{Tab}
			
		}
		
	}
	
	Else If WinActive("ahk_class MozillaWindowClass") {
		
		Send, {Ctrl Down}{PgDn}{Ctrl Up}
		
	}
	
	
	Else If WinActive("ahk_class rctrl_renwnd32") {
		
		Send, {Down}
		
	}
	
	Else If WinActive("ahk_class AcrobatSDIWindow") {
		
		Send, ^{Tab}
		
	}
	
    ;Spotify
	Else If WinActive("ahk_class Chrome_WidgetWin_0") {
		
		Send, {Ctrl down}
		Send, {Right}
		Send, {Ctrl up}
		
	}
	
	Else If WinActive("ahk_class Framework::CFrame") {
		
		Send, ^{Tab}
		
	}
	
	Else {
		
		;to do more than one case at a time use Case 2, 3:
		Switch MachineChoice
		{
			Case -1:	;BLANK
			
			
			Case 0:		;TEST
			
			
			Case 1:		;Work - Work PC
			
			switchDesktopToRight()
			TrayTip, Desktop, Desktop %CurrentDesktop%.
			SetTimer, HideTrayTip, -2000
			
			
			Case 2:		;Work - Home PC
			
			switchDesktopToRight()
			TrayTip, Desktop, Desktop %CurrentDesktop%.
			SetTimer, HideTrayTip, -2000
			
			
			Case 3:		;Work - Surface
			
			Send, {LCtrl down}{LWin down}
			Sleep, 5 
			Send, {Right}
			Send, {LWin up}{LCtrl up}
			
			TrayTip, Desktop, Desktop %CurrentDesktop%.
			SetTimer, HideTrayTip, -2000
			
			
			Case 4:		;Home - Home PC
			
			switchDesktopToRight()
			TrayTip, Desktop, Desktop %CurrentDesktop%.
			SetTimer, HideTrayTip, -2000
			
			
			Case "4r":	;Home - Gaming (Rimworld)
			
			
			Case 5:		;Home - Surface
			
			Send, {LCtrl down}{LWin down}
			Sleep, 5 
			Send, {Right}
			Send, {LWin up}{LCtrl up}
			
			TrayTip, Desktop, Desktop %CurrentDesktop%.
			SetTimer, HideTrayTip, -2000
			
			
			Default:
			msgbox case error
		}
		
	}
	
}
Return 

;-------------------------------------------------------------
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
Post Reply

Return to “Ask For Help”