return key pressed: MyCalclulator

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
HeXaDeCiMaToR
Posts: 155
Joined: 08 Feb 2021, 12:42

return key pressed: MyCalclulator

26 Sep 2021, 18:37

I'm trying to complete a calculator that I wrote, but I'm finding that 1) My script could be longer than I need, and 2) If I'm not in the target active window my buttons aren't working as normal.

For example: When I'm in the target active window, if I have "~" in front of the number/operator Hotkeys ("$1", "$-") then windows dings when I press the desired calculator keyboard buttons (only when the target window, my gui calculator, is active), but then I don't need the send, "Keys" section and everything works perfectly (with the exception of the DING). Also, when I hijack the NumpadSub for this, it doesn't comment things out in SciTE anymore.

Here's the code for One and Dash/Minus/Subtract:

Code: Select all

$1::
$Numpad1::
_pfc:=0 ;; OFF
WinGetTitle,var,A,,
if (var="target active window")
{
	GuiControlGet,var,,mathText
	var.= "1"
	GuiControl,,mathText, %var%
}
else
	Send, 1
_pfc:=1 ;; ON
return

Code: Select all

$NumpadSub::
_pfc:=0 ;; OFF
WinGetTitle,var,A,,
if (var="target active window")
{
	GuiControlGet,var,,mathText
	GuiControlGet,result,,guiEdit_Result
	if (result="")
	{
		var.= " - "
		GuiControl,,mathText, %var%
		GuiControl,,mathText, %var%
	}
	else
	{
		var:=""
		var.= result " - "
		GuiControl,,mathText, %var%
	}
}
else
	Send, -
_pfc:=1 ;; ON
return
Is there a way to write a #ifWinActive expression that gets the key pressed and if that key is [0-9]||(\/|\*|\+|\-) return that key, and put it into var.= for the mathText control to evaluate? OR is there a better way to achieve this goal?

I know my issue with SciTE is the Send, "Key". I need a method that if not in the target active window it will act as normal, and not ding at me when IN the target active window...

_pfc is a variable that I have set to activate a SetTimer (not related to the issue).

Any help would be appreciated!!
HeXaDeCiMaToR
Posts: 155
Joined: 08 Feb 2021, 12:42

Re: return key pressed: MyCalclulator

26 Sep 2021, 18:50

I'm not opposed to creating an associative array that tells the script what to enter into mathText.

such as:

Code: Select all

Keypressed:={1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,0:0,Numpad1:1,Numpad2:2,Numpad3:3,Numpad4:4,Numpad5:5,Numpad6:6,Numpad7:7,Numpad8:8,Numpad9:9,Numpad0:0,NumpadDiv:/,NumpadMult:*,NumpadAdd:+,NumpadSub:-}
...and so on...
User avatar
mikeyww
Posts: 26886
Joined: 09 Sep 2014, 18:38

Re: return key pressed: MyCalclulator

26 Sep 2021, 18:58

Code: Select all

#IfWinActive ahk_exe notepad.exe
1::
2::
3::
4::
5::
6::
7::
8::
9::
0::
/::
*::
+::
-::
var .= A_ThisHotkey
MsgBox, 64, var, %var%
Return
#IfWinActive
HeXaDeCiMaToR
Posts: 155
Joined: 08 Feb 2021, 12:42

Re: return key pressed: MyCalclulator

26 Sep 2021, 19:19

That looks pretty simple. I'll give it a try and post the results.

As always, Thank you @mikeyww !
HeXaDeCiMaToR
Posts: 155
Joined: 08 Feb 2021, 12:42

Re: return key pressed: MyCalclulator

26 Sep 2021, 19:33

I tried a simple(r) test and I'm not getting any msgbox...

Code: Select all

#IfWinActive ahk_exe AutoHotkey.exe
MsgBox window active
return
#IfWinActive
What am I missing?

EDIT:
I updated the script to grab the title and then msgbox the result.

Code: Select all

#IfWinActive ahk_exe AutoHotkey.exe
WinGetTitle,var,A,,
MsgBox Active Window %var%
return
#IfWinActive
EDIT:

Code: Select all

I just figured out that I still need a hotkey to trigger the event
#IfWinActive ahk_exe AutoHotkey.exe
1::
WinGetTitle,var,A,,
MsgBox Active Window %var%
return
#IfWinActive
HeXaDeCiMaToR
Posts: 155
Joined: 08 Feb 2021, 12:42

Re: return key pressed: MyCalclulator

26 Sep 2021, 21:15

My finished code:

Code: Select all

#IfWinActive, target active window
1::
Numpad1::
2::
Numpad2::
3::
Numpad3::
4::
Numpad4::
5::
Numpad5::
6::
Numpad6::
7::
Numpad7::
8::
Numpad8::
9::
Numpad9::
0::
Numpad0::
.::
NumpadDot::
^8::
NumpadMult::
-::
NumpadSub::
^=::
NumpadAdd::
/::
NumpadDiv::
Enter::
NumpadEnter::
Del::
BackSpace::
key:=A_ThisHotkey
kp:={"Numpad1":"1","Numpad2":"2","Numpad3":"3","Numpad4":"4","Numpad5":"5","Numpad6":"6","Numpad7":"7","Numpad8":"8","Numpad9":"9","Numpad0":"0","^8":"*","NumpadMult":"*","NumpadSub":"-","^=":"+","NumpadAdd":"+","NumpadDiv":"/","NumpadEnter":"Enter","NumpadDot":"."}
for k,v in kp
	if (key=k)
		key:=v
if (key="1")||(key="2")||(key="3")||(key="4")||(key="5")||(key="6")||(key="7")||(key="8")||(key="9")||(key="0")||(key=".")
{
	GuiControlGet,var,,mathText
	var.= key
	GuiControl,,mathText, %var%
}
if (key="/")||(key="*")||(key="+")||(key="-")
{
	GuiControlGet,var,,mathText
	GuiControlGet,result,,guiEdit_Result
	if (result="")
	{
		var.= " " key " "
		GuiControl,,mathText, %var%
	}
	else
	{
		var:=""
		var.= result  " " key " "
		GuiControl,,mathText, %var%
	}
}
if (key="Enter")
{
	GuiControlGet,var,,mathText
	Num:=StrSplit(var," ")
	if (Num[2]="/")
		ans:= Num[1] / Num[3]
	else if (Num[2]="*")
		ans:= Num[1] * Num[3]
	else if (Num[2]="-")
		ans:= Num[1] - Num[3]
	else if (Num[2]="+")
		ans:= Num[1] + Num[3]
	else
		ans:=var
	SetFormat, Float, 0.2
	ans+=0
	if 	InStr(var," ")
	{
		ans:=RegExReplace(ans,"\.00","")
		Num1:=Num[1]
		mathFunc:=Num[2]
		Num3:=Num[3]
		var= %Num1% %mathFunc% %Num3% = %ans%
	}
	GuiControl,,mathText, %var%
	GuiControl,,guiEdit_Result, %ans%
	var:=""
}
if (key="Del")
	{
	mEmpty:=""
	GuiControl,,mathText, %mEmpty%
	GuiControl,,guiEdit_Result, %mEmpty%
	GuiControl,,ahCalc_BM,%mEmpty%
	GuiControl,,ahCalc_PVA,%mEmpty%
	GuiControl,,ahCalc_ADR,%mEmpty%
	var:=""
	Num1:=""
	mathFunc:=""
	Num2:=""
}
if (key="BackSpace")
	{
	GuiControlGet,varmt,,mathText
	StringTrimRight,varmt,varmt,1
	GuiControl,,mathText,%varmt%
	varmt:=""
}
return
#IfWinActive
I've still got to figure out how to integrate my associated buttons, but at least the keyboard entry is working like a charm!
HeXaDeCiMaToR
Posts: 155
Joined: 08 Feb 2021, 12:42

Re: return key pressed: MyCalclulator

26 Sep 2021, 21:17

Code: Select all

Gui, Add, Button, x20 y+0 w105 Disabled h34 gDel,Clear

Gui, Add, Button, x135 y290 w50 h50 vCalcSeven g7, 7
Gui, Add, Button, x135 y+10 w50 h50 vCalcFour g4, 4
Gui, Add, Button, x135 y+10 w50 h50 vCalcOne g1, 1

Gui, Add, Button, x195 y290 w50 h50 vCalcEight g8, 8
Gui, Add, Button, x195 y+10 w50 h50 vCalcFive g5, 5
Gui, Add, Button, x195 y+10 w50 h50 vCalcTwo g2, 2

Gui, Add, Button, x255 y290 w50 h50 vCalcNine g9, 9
Gui, Add, Button, x255 y+10 w50 h50 vCalcSix g6, 6
Gui, Add, Button, x255 y+10 w50 h50 vCalcThree g3, 3

Gui, Add, Button, x315 y290 w50 h42.5 gNumpadDiv, /
Gui, Add, Button, x315 y+0 w50 h42.5 gNumpadMult, *
Gui, Add, Button, x315 y+0 w50 h42.5 gNumpadSub, -
Gui, Add, Button, x315 y+0 w50 h42.5 gNumpadAdd, +

Gui, Add, Button, x20 y470 w105 h50 gBackSpace, Backspace
Gui, Add, Button, x135 y470 w50 h50, +/-
Gui, Add, Button, x195 y470 w50 h50 g0, 0
Gui, Add, Button, x255 y470 w50 h50 gNumpadDot, .
Gui, Add, Button, x315 y470 w50 h50 gEnter, =

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Decar, doodles333, mikeyww and 222 guests