long/short keypress - avoid Windows ding?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

long/short keypress - avoid Windows ding?

08 May 2021, 07:59

I have this code to activate a different function depending upon whether a key press is short or long:

Code: Select all

q:: 
KeyWait, %A_ThisHotkey%, T0.75
errorlevel ? Function1() : Function(2)
return
The problem is that when the key press is long and I get an errorlevel I also hear a windows ding. How to avoid the ding? Is there a better approach to getting two results dependent upon the length of key press? Thanks.
User avatar
mikeyww
Posts: 26592
Joined: 09 Sep 2014, 18:38

Re: long/short keypress - avoid Windows ding?

08 May 2021, 08:03

Yeah, fix your functions (which are not provided here)!
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: long/short keypress - avoid Windows ding?

08 May 2021, 08:10

Thanks Mikey, but if I send the same function I get nothing with the short key press but I get a ding with the long key press suggesting to me that it is not the function but the method:

Code: Select all

q:: 
KeyWait, %A_ThisHotkey%, T0.75
errorlevel ? Function(2) : Function(2) ;ding/no ding
return
Last edited by PuzzledGreatly on 08 May 2021, 09:10, edited 1 time in total.
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: long/short keypress - avoid Windows ding?

08 May 2021, 08:56

I'm not hearing a ding no matter how long I press the key with the following:

Code: Select all

q::
KeyWait, %A_ThisHotkey%, T0.75
errorlevel ? Function(1) : Function(2)
return

Function(n) {
	ToolTip, % n
}
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: long/short keypress - avoid Windows ding?

08 May 2021, 09:14

Thanks boiler, I tried your code and got a ding. How long did you hold the key for?

Edit: I used this code:

Code: Select all

q::
KeyWait, %A_ThisHotkey%, T0.75
errorlevel ? Function(1) : Function(2)
return

Function(n) {
	msgbox, 4096, OK, % n
}
Last edited by PuzzledGreatly on 08 May 2021, 09:23, edited 1 time in total.
User avatar
mikeyww
Posts: 26592
Joined: 09 Sep 2014, 18:38

Re: long/short keypress - avoid Windows ding?

08 May 2021, 09:23

Is this the entire script? Are other scripts running at the same time?
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: long/short keypress - avoid Windows ding?

08 May 2021, 09:29

Thanks, Mikey, when I isolate the q hotkey I don't get a ding. But how can I track down what is causing the ding? I don't think it can be boiler's amended function with the msgbox. What kind of things should I be looking for? My whole script is over 1000 lines long.
User avatar
mikeyww
Posts: 26592
Joined: 09 Sep 2014, 18:38

Re: long/short keypress - avoid Windows ding?

08 May 2021, 09:35

You can isolate the problem within a few minutes, by removing half of the remaining script, retesting, and repeating the process until the sound disappears. You will then have pinpointed the problem.
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: long/short keypress - avoid Windows ding?

08 May 2021, 09:45

Thanks, Mikey, but my script doesn't lend itself to that approach. I'm going to sleep on it and in the morning look at the functions I call in detail
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: long/short keypress - avoid Windows ding?

08 May 2021, 10:07

PuzzledGreatly wrote: Thanks boiler, I tried your code and got a ding. How long did you hold the key for?
I can hold it for a very long time (10 seconds or more), and still no ding. I tried your script with the MsgBox as well and it's the same thing.

I wonder if it might be this...Perhaps certain ding sounds are "empty" on some systems but not on others. For example, I don't get a ding when I produce a MsgBox with the question mark icon like below, but I thought I remember it producing a ding on previous versions of Windows I had. Do you get a ding with the following?

Code: Select all

MsgBox, 32, Ding?, Does this MsgBox produce a ding?
User avatar
mikeyww
Posts: 26592
Joined: 09 Sep 2014, 18:38

Re: long/short keypress - avoid Windows ding?

08 May 2021, 10:21

Sounds can be altered if needed:

C:\Windows\System32\rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,2
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: long/short keypress - avoid Windows ding?

08 May 2021, 18:12

Thanks, boiler, I don't get a sound with your msgbox code. With the following script I get a ding with any letter key except q. Do you?

Code: Select all

#NoEnv
#Warn
SendMode Input
SetWorkingDir %A_ScriptDir%
SetBatchLines, -1
#Persistent
#SingleInstance force

Gui, New, -dpiscale -caption border, Ding Tester
Gui, font, s20 bold
Gui, add, text, w600 center, Do you hear a ding when you press q?
Gui, add, text, w600 center, Do you hear a ding pressing other keys?
Gui, show, xcenter y200

return

q:: MsgBox, 32, Ding?, Does this MsgBox produce a ding?

esc:: exitapp
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: long/short keypress - avoid Windows ding?

08 May 2021, 18:58

PuzzledGreatly wrote: With the following script I get a ding with any letter key except q. Do you?
Yes, same here. Ding with any letter key except q.
User avatar
mikeyww
Posts: 26592
Joined: 09 Sep 2014, 18:38

Re: long/short keypress - avoid Windows ding?

08 May 2021, 19:04

The "question" MsgBox designated by option 32 does not make a sound, by default. At least that is the case on my computer. There is a Windows "Utopia Question" WAV file that was used on Windows 7. I do not know whether Windows 10 changed this-- did change some of the sounds-- and also do not know whether the silence of the MsgBox is "intended" by the developer, but it is silent on my system.
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: long/short keypress - avoid Windows ding?

08 May 2021, 19:33

Thanks for the info, Mikey. This seems to be the solution on my PC. No Ding:

Code: Select all

q:: 
keywait q
a_timeSinceThisHotkey > 750 ? function1() : function2()
return

function1() {
msgbox, function1
}

function2() {
msgbox, function 2
}
User avatar
mikeyww
Posts: 26592
Joined: 09 Sep 2014, 18:38

Re: long/short keypress - avoid Windows ding?

08 May 2021, 20:37

Sure, if you provide no options, there will be no sound.
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: long/short keypress - avoid Windows ding?

10 May 2021, 07:51

But the original method I posted using errorlevel did produce a ding with my machine, hence this thread.
User avatar
mikeyww
Posts: 26592
Joined: 09 Sep 2014, 18:38

Re: long/short keypress - avoid Windows ding?

10 May 2021, 08:10

You get a ding because that is what happens when you show a GUI and press a key. Demonstration is below.

Code: Select all

Gui, Add, Text, TEST
Gui, Show, w300, Test
This GUI cannot accept a key, so it plays a nice ding for you to notify you about that.

When you have a hotkey assigned, the hotkey routine will run instead. If your hotkey routine does not play a ding, then you will not hear any. If your hotkey routine is designed to play a ding, such as a MsgBox with an option for a ding sound, then you will hear it.
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: long/short keypress - avoid Windows ding?

13 May 2021, 07:56

Mikey, I think your last post was not helpful and irrelevant. The point is that the method used to assess the long key press using errorlevel gave a ding but that using A_timeSinceThisHotkey did not. That's it.
User avatar
mikeyww
Posts: 26592
Joined: 09 Sep 2014, 18:38

Re: long/short keypress - avoid Windows ding?

13 May 2021, 08:07

Sorry if I missed the point. I was actually working with the script that you posted in explaining the exact findings, so I believed that I had completely explained your experience. It looks like I didn't!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], jaka1, marypoppins_1, Rohwedder and 136 guests