MsgBox help

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

MsgBox help

Post by LAPIII » 21 Jan 2022, 15:24

I found this useful script to detects the Numlock state, displays a message box, and plays a sound:

Code: Select all

~Numlock::  ; detect NumLock without blocking it (~)
    if (GetKeyState("NumLock", "T"))  ; get the toggle-state of NumLock
        MsgBox, Numlock is on
    else
        MsgBox, Numlock is off
    return
   
I wanted to make one for the Capslock, but it doesn't play sound:

Code: Select all

~Capslock::  ; detect Capslock without blocking it (~)
    if (GetKeyState("NumLock", "T"))  ; get the toggle-state of NumLock
        MsgBox, Capslock is on
    else
        MsgBox, nUMock is off
    return
   

User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: MsgBox help

Post by Xtra » 21 Jan 2022, 15:29

Your code has errors try this:

Code: Select all

~Capslock::  ; detect Capslock without blocking it (~)
    if (GetKeyState("Capslock", "T"))  ; get the toggle-state of Capslock
        MsgBox, Capslock is on
    else
        MsgBox, Capslock is off
    return

LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: MsgBox help

Post by LAPIII » 26 Jan 2022, 09:26

bump

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: MsgBox help

Post by boiler » 26 Jan 2022, 10:06

Why are you bumping this without any other comment? Did you not see Xtra's reply?

Also, I don't see why your first script would play a sound. It wouldn't unless you have something else going on that would play a sound when you press NumLock.

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: MsgBox help

Post by amateur+ » 26 Jan 2022, 10:45

Code: Select all

~Capslock::  ; detect Capslock without blocking it (~)
	CoordMode, ToolTip
	ToolTip, % "Capslock is " (OnOff := (GetKeyState("Capslock", "T") ? "On" : "Off"))
		, % A_ScreenWidth/2, % A_ScreenHeight/2, 9
	SetTimer, ToolTipOff, -2000
	SetTimer, Voice, -10
	return

ToolTipOff:
	ToolTip,,,, 9
	return
	
Voice:
	VoiceText("Capslock is " . OnOff . ", my lord!")	; Soundbeep, 1000	; You can use this primitive alternative.
	return
	
VoiceText(pTextToSpeak := "Good") {
	static msg := ComObjCreate("SAPI.SpVoice")
	msg.Speak(pTextToSpeak)
}
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.

LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: MsgBox help

Post by LAPIII » 26 Jan 2022, 15:27

The error I got:

Image

[EDIT] I just ran the Numlock script and did it here as sound, even after a restart.

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: MsgBox help

Post by amateur+ » 26 Jan 2022, 16:25

@LAPIII copy/paste here the whole script you ran and got an error. Or you can attach the ahk-file here in attachments.
I'm pretty sure you haven't copy/paste the whole my script and missed this part:

Code: Select all

Voice:
	VoiceText("Capslock is " . OnOff . ", my lord!")	; Soundbeep, 1000	; You can use this primitive alternative.
	return
	
VoiceText(pTextToSpeak := "Good") {
	static msg := ComObjCreate("SAPI.SpVoice")
	msg.Speak(pTextToSpeak)
}
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.

LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: MsgBox help

Post by LAPIII » 26 Jan 2022, 22:10

I never got an error with my scripts. When I ran yours, I got:

Image

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: MsgBox help

Post by boiler » 26 Jan 2022, 22:16

That's not an error, and it's not his script. Don't say this is what you got when you run his script if you're adding other lines to it. That's your modified version of his script. His script doesn't have #Warn in it, nor does it have some other lines your script apparently has according to the warning message.

LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: MsgBox help

Post by LAPIII » 26 Jan 2022, 22:29

Sorry, I had a couple of directives in my test script.

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: MsgBox help

Post by amateur+ » 27 Jan 2022, 00:20

Thank you, boiler.
@LAPIII, press on "Select All" and then "Ctrl+C", then paste into a new empty ahk-file, save it and run. Then report about results.

Code: Select all

~Capslock::  ; detect Capslock without blocking it (~)
	CoordMode, ToolTip
	ToolTip, % "Capslock is " (OnOff := (GetKeyState("Capslock", "T") ? "On" : "Off"))
		, % A_ScreenWidth/2, % A_ScreenHeight/2, 9
	SetTimer, ToolTipOff, -2000
	SetTimer, Voice, -10
	return

ToolTipOff:
	ToolTip,,,, 9
	return
	
Voice:
	VoiceText("Capslock is " . OnOff . ", my lord!")	; Soundbeep, 1000	; You can use this primitive alternative.
	return
	
VoiceText(pTextToSpeak := "Good") {
	static msg := ComObjCreate("SAPI.SpVoice")
	msg.Speak(pTextToSpeak)
}
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.

LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: MsgBox help

Post by LAPIII » 27 Jan 2022, 12:25

It works perfectly!

LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: MsgBox help

Post by LAPIII » 13 Feb 2022, 11:30

@amateur+, with line 15 in your script, VoiceText("Capslock is " . OnOff . ", my lord!") I don't know how to take out ", my lord!", so I changed it to VoiceText("Capslock is " . OnOff . "`t"). Can you help me?

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: MsgBox help

Post by boiler » 13 Feb 2022, 12:01

Just remove the part you don't want, which is the literal string on the end and the . concatenation operator:

Code: Select all

VoiceText("Capslock is " . OnOff)

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: MsgBox help

Post by amateur+ » 14 Feb 2022, 09:52

@LAPIII, hello! You will make me happy if you study this small concept:
Spoiler
Have a good day!
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.

LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: MsgBox help

Post by LAPIII » 23 Feb 2022, 12:14

I put a shortcut of this in my startup folder:

Code: Select all

#SingleInstance, Force
~CapsLock::  ; detect Capslock without blocking it (~)
	CoordMode, ToolTip
	ToolTip, % "CapsLock is " (OnOff := (GetKeyState("CapsLock", "T") ? "ON" : "OFF"))
		, % A_ScreenWidth/2, % A_ScreenHeight/2, 9
	SetTimer, ToolTipOff, -2000
	SetTimer, Voice, -10
	return

ToolTipOff:
	ToolTip,,,, 9
	return
	
Voice:
	VoiceText("CapsLock is " . OnOff)
	return
	
VoiceText(pTextToSpeak := "Good") {
	static msg := ComObjCreate("SAPI.SpVoice")
	msg.Speak(pTextToSpeak)
}
Now every time that I press CapsLock this script gets it wrong, i.e. If I press it once then it says that CapsLock is OFF and pressing it against says CapsLock is ON. I figure that the problem is that the keyboard drivers on this computer load too soon to when this script is activated.

User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: MsgBox help

Post by Xtra » 23 Feb 2022, 12:35

Now every time that I press CapsLock this script gets it wrong
You can set the CapsLock state when the script is run using: SetCapsLockState (put at top of script)

LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: MsgBox help

Post by LAPIII » 23 Feb 2022, 13:52

I see what's up with this script, it can get the same position twice for one position.

LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: MsgBox help

Post by LAPIII » 23 Feb 2022, 15:08

Here's a script that I found on ExpertsExchange (The icons are on that page):

Code: Select all

; Joe Winograd 17-May-2019
#Warn,UseUnsetLocal ; warning on uninitialized variables
#NoEnv ; avoid checking empty variables to see if they are environment variables
#SingleInstance Force ; replace old instance immediately
SetBatchLines,-1 ; run at maximum speed

Gosub,InitializeVars ; initialize all variables
Gosub,ConfigureInitialTray ; configure initial system tray (notification area)

CapsLock:: ; catch CapsLock key
KeyWait,CapsLock ; CapsLock pressed, but wait until user releases it
Gosub,ToggleCapsLock ; CapsLock released, now toggle its state
Return

InitializeVars:
TrayIconON:=A_ScriptDir . "\CapsLockON.ico" ; icon to display in tray when CapsLock is on
TrayIconOFF:=A_ScriptDir . "\CapsLockOFF.ico" ; icon to display in tray when CapsLock is off
Return

ConfigureInitialTray:
Menu,Tray,NoStandard ; do not show standard AutoHotkey tray context menu
Menu,Tray,Add,&Toggle CapsLock,ContextMenu ; first context menu choice is Toggle (T is access key)
Menu,Tray,Add,E&xit,ContextMenu ; second context menu choice Exit (X is access key)
Menu,Tray,Default,&Toggle CapsLock ; Toggle is default
Menu,Tray,Click,1 ; allow a single-click instead of a double-click to toggle CapsLock
Gosub,GetKeyStateIconTip ; get current state of CapsLock and set tray icon and tray tip accordingly
Menu,Tray,Icon,%TrayIcon% ; display On or Off tray icon
Menu,Tray,Tip,%TrayTip% ; display On or Off tray tip
Return

GetKeyStateIconTip:
If (GetKeyState("CapsLock","T")) ; check CapsLock state
{
  TrayIcon:=TrayIconON ; CapsLock is On - set tray icon accordingly
  TrayTip:="CapsLock ON" ; CapsLock is On - set tray tip accordingly
}
Else
{
  TrayIcon:=TrayIconOFF ; CapsLock is Off - set tray icon accordingly
  TrayTip:="CapsLock OFF" ; CapsLock is Off - set tray tip accordingly
}
Return

ToggleCapsLock:
If (GetKeyState("CapsLock","T")) ; check CapsLock state
{
  SetCapsLockState,Off ; CapsLock is On - turn it Off
  TrayIcon:=TrayIconOFF ; Set tray icon to Off
  TrayTip:="CapsLock OFF" ; ; Set tray tip to Off
}
Else
{
  SetCapsLockState,On  ; CapsLock is Off - turn it On
  TrayIcon:=TrayIconON ; Set tray icon to On
  TrayTip:="CapsLock ON" ; Set tray tip to On
}
Menu,Tray,Icon,%TrayIcon% ; display On or Off tray icon
Menu,Tray,Tip,%TrayTip% ; display On or Off tray tip
Return

ContextMenu:
; check which context menu selected
If (A_ThisMenuItem="&Toggle CapsLock") ; true means Toggle selected
{
  Gosub,ToggleCapsLock ; toggle CapsLock state
  Return
}
If (A_ThisMenuItem="E&xit") ; true means Exit selected, but make sure it was not accidental
{
  MsgBox,4388,Terminate CapsLockOSD?,Are you sure you want to quit? ; 4388 means No is default button, in case of accidental Enter key
  IfMsgBox,No
    Return ; user does not really want to quit - stay running
}
ExitApp ; user wants to quit - terminate app
I need help, This didn't work:

Code: Select all

; Joe Winograd 17-May-2019
#Warn,UseUnsetLocal ; warning on uninitialized variables
#NoEnv ; avoid checking empty variables to see if they are environment variables
#SingleInstance Force ; replace old instance immediately
SetBatchLines,-1 ; run at maximum speed

CapsLock:: ; catch CapsLock key
KeyWait,CapsLock ; CapsLock pressed, but wait until user releases it
Gosub,ToggleCapsLock ; CapsLock released, now toggle its state
Return

ToggleCapsLock:
If (GetKeyState("CapsLock","T")) ; check CapsLock state
{
  SetCapsLockState,Off ; CapsLock is On - turn it Off
  ToolTip:="CapsLock is ON" 
}
Else
{
  SetCapsLockState,On  ; CapsLock is Off - turn it On
 
  ToolTip:="CapsLock is ON" 
}

ExitApp ; user wants to quit - terminate app

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: MsgBox help

Post by boiler » 23 Feb 2022, 15:40

You have it so it always exits the app at the end of the ToggleCapsLock subroutine. Do you see why? Do you see the differences between your script and Joe's that would cause that to be true?

By the way, when you significantly change someone's script like that, it is misleading to leave their name as the author at the top with no other comment. @JoeWinograd wouldn't have made the mistakes that your script has.

Also notice that both of your ToolTip statements say that that CapsLock is on, and you don't do anything to eventually remove the tooltip.

Post Reply

Return to “Ask for Help (v1)”