Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

SinglePress, DoublePress and PressAndHold


  • Please log in to reply
15 replies to this topic
emmanuel d
  • Members
  • 519 posts
  • Last active: Jul 15 2017 12:04 PM
  • Joined: 29 Jan 2009
script for hotkeys that supports, SinglePress, DoublePress and PressAndHold:
#singleinstance force
DoubleClickTime := DllCall("GetDoubleClickTime")	; Get the doubleclicktime in milliseconds
MsgBox,DoubleClickTime=%DoubleClickTime%ms			; Show it to the user
return
#c::												; Win+c Supports SingleClick, DoubleClick and ClickAndHold
	KeyWait,c										; wait until first keypres is ended
	if (A_TimeSinceThisHotkey>DoubleClickTime) {	; if the hotkey is longer pressed than the time to doubleclick
		msgbox,Longpress							; Do some action
		return										; No need to do something else, so return
	}
	KeyWait, c,% "d T"DoubleClickTime/1000			; Wait for a new pres of c, x nr of seconds
	If ! Errorlevel									; If no error returned there was a doublepress or click
		MsgBox,doubleclick							; Than do some action
	else MsgBox,singleclick							; If there was a timeout(error) means, no double , but singleclick, so do a action
	Return											; We are donne so, return

Have fun.
Different one:
#singleinstance force
DoubleClickTime := DllCall("GetDoubleClickTime")	; Get the doubleclicktime in milliseconds
^c::												; ctrl+c Supports SingleClick, DoubleClick and ClickAndHold
	KeyWait,c										; wait until first keypres is ended
	if (A_TimeSinceThisHotkey>DoubleClickTime) {	; if the hotkey is longer pressed than the time to doubleclick
		msgbox,Longpress							; Do some action
		return										; No need to do something else, so return
		}
	if (A_ThisHotkey = A_PriorHotkey && A_TimeSincePriorHotkey < DoubleClickTime) {
		MsgBox,Doubleclick
		Return
		}
	if !GetKeyState("Ctrl") {
		MsgBox,Singleclick
		Return
		}
	Return											; We are donne so, return

Stopwatch emdkplayer
the code i post falls under the: WTFYW-WTFPL license

http://www.ahkscript.org/ the new forum


noise
  • Members
  • 57 posts
  • Last active: Aug 31 2018 10:16 AM
  • Joined: 14 May 2009

#c:: ; ctrl+c Supports SingleClick, DoubleClick and ClickAndHold


That should be Win+c :)
PrimalNoise.com
It's a rock. Can't wait to tell my friends. They don't have a rock this big.

emmanuel d
  • Members
  • 519 posts
  • Last active: Jul 15 2017 12:04 PM
  • Joined: 29 Jan 2009
Fixed bug in comment :wink:

Stopwatch emdkplayer
the code i post falls under the: WTFYW-WTFPL license

http://www.ahkscript.org/ the new forum


emmanuel d
  • Members
  • 519 posts
  • Last active: Jul 15 2017 12:04 PM
  • Joined: 29 Jan 2009
Added new code on first post.

Stopwatch emdkplayer
the code i post falls under the: WTFYW-WTFPL license

http://www.ahkscript.org/ the new forum


Wicked - Guest
  • Guests
  • Last active:
  • Joined: --
Very neat. I like it! I've just never thought of using hotkeys like that. :D.

SoerenB
  • Members
  • 10 posts
  • Last active: Apr 14 2010 09:30 AM
  • Joined: 25 Sep 2008
A good one, congratulations!

I replaced #c:: with
just MButton (no modifier!)
in the upper example.
now my MButton has a long- and a double- click!

[Edit]
Someone PMmed me for the code, so here it is:
>!WARNING!< do NOT use with R or LButton. Haven't tried more buttons.

; by emmanuel d, modified by SoerenB @ http://www.autohotkey.com/forum/topic55562.html
; (I detached the KeyWait times from the System's doubleckick time - edit "345" and "0.345" to suit your needs)
; (>!WARNING!<do NOT use with R or LButton: losing the dragging action is the least, but not the only consequence)
; the tilde (~)  keeps the Autoscroll enabled. If you don't use that anyway, you do not need the tilde.
; longClick in some windows needs 2 clicks with the tilde present

~MButton::                                    ; MButton Supports SingleClick, DoubleClick and ClickAndHold
   KeyWait,MButton                              ; wait until buttonpress is ended
   if (A_TimeSinceThisHotkey>345) {   ; if the hotkey is longer pressed than 345 miliseconds
; my action (replace with yours)
    Winset, AlwaysOnTop, Toggle, A                 ; toggle Window topmost
    WinGet, ExStyle, ExStyle, A
    If (ExStyle & 0x8)    ; 0x8 is WS_EX_TOPMOST.
    WinSet, Transparent, 230, A		;if topmost, make it transparent just a bit, as visual feedback for "topmost"
    else
    WinSet, Transparent, 255, A
; end of my action
      return                              ; No need to do something else, so return
   }
   KeyWait, MButton, D T0.345      ; Wait for a new press of MButton, for 0.345 seconds
   If ! Errorlevel                           ; If no error returned there was a doublepress or click
; my action (replace with yours)
   WinSet, Style, ^0x80800000, A
   else 
; Send the MButton now to regain its original function:  
;   Send,  {MButton up}         ; If there was a timeout(error) means, no double , but singleclick
; end of my action    
;   Return                                 ; We are donne so, return

Thank you,
Cheers,
SoerenB

leehong1
  • Validating
  • 403 posts
  • Last active: May 19 2019 03:12 PM
  • Joined: 24 Nov 2008
emmanuel d..... i tried using spacebar for your code...it does not work for

the second example with the

if !GetKeyState("Space")




double press of spacebar still register as a single press...
#singleinstance force
DoubleClickTime := DllCall("GetDoubleClickTime")   ; Get the doubleclicktime in milliseconds
Space::                                  
   KeyWait,Space                              
   if (A_TimeSinceThisHotkey>DoubleClickTime)
      {					   
      msgbox,Longpress                  
      return                              
      }
   if (A_ThisHotkey = A_PriorHotkey && A_TimeSincePriorHotkey < DoubleClickTime)
      {
      ControlClick, x235 y710, a
      Return	
      }
   if !GetKeyState("Space")
      {
      Send, {f5}
      Return
      }


emmanuel d
  • Members
  • 519 posts
  • Last active: Jul 15 2017 12:04 PM
  • Joined: 29 Jan 2009
this works fine here:
#singleinstance force
DoubleClickTime := DllCall("GetDoubleClickTime")   ; Get the doubleclicktime in milliseconds
Space::                                
   KeyWait,Space                             
   if (A_TimeSinceThisHotkey>DoubleClickTime)
      {                  
      msgbox,Longpress                 
      return                             
      }
   if (A_ThisHotkey = A_PriorHotkey && A_TimeSincePriorHotkey < DoubleClickTime)
      {
      msgbox,doublepress
      Return   
      }
no need for:
if !GetKeyState("Space")
      {
      Send, {f5}
      Return
      }
it is for modifier keys

shortversion:
#singleinstance force
DoubleClickTime := DllCall("GetDoubleClickTime")   ; Get the doubleclicktime in milliseconds
Space::                                
   KeyWait,Space                             
   if (A_TimeSinceThisHotkey>DoubleClickTime)                 
      msgbox,Longpress                 
   else if (A_ThisHotkey = A_PriorHotkey && A_TimeSincePriorHotkey < DoubleClickTime)
      msgbox,doublepress
     Return   
or you might want the space to pass thru, like:
#singleinstance force
DoubleClickTime := DllCall("GetDoubleClickTime")   ; Get the doubleclicktime in milliseconds
$Space::                                
   KeyWait,Space                             
   if (A_TimeSinceThisHotkey>DoubleClickTime)                 
      msgbox,Longpress                 
   else if (A_ThisHotkey = A_PriorHotkey && A_TimeSincePriorHotkey < DoubleClickTime)
      msgbox,doublepress
	else send {space}
   Return   

Stopwatch emdkplayer
the code i post falls under the: WTFYW-WTFPL license

http://www.ahkscript.org/ the new forum


emmanuel d
  • Members
  • 519 posts
  • Last active: Jul 15 2017 12:04 PM
  • Joined: 29 Jan 2009
test with:
#singleinstance force

DoubleClickTime := DllCall("GetDoubleClickTime")   ; Get the doubleclicktime in milliseconds

$Space::                                

   KeyWait,Space                             

   if (A_TimeSinceThisHotkey>DoubleClickTime)                 

      Tooltip,Longpress                 

   else if (A_ThisHotkey = A_PriorHotkey && A_TimeSincePriorHotkey < DoubleClickTime)

      tooltip,doublepress

	else tooltip,Singlepress

   Return   


Stopwatch emdkplayer
the code i post falls under the: WTFYW-WTFPL license

http://www.ahkscript.org/ the new forum


leehong1
  • Validating
  • 403 posts
  • Last active: May 19 2019 03:12 PM
  • Joined: 24 Nov 2008
emmanuel d....thanks ...i will try it later

leehong1
  • Validating
  • 403 posts
  • Last active: May 19 2019 03:12 PM
  • Joined: 24 Nov 2008
emmanuel d...
tried your revamp code.....it's wonderful.......many...many....thank you..

leehong1
  • Validating
  • 403 posts
  • Last active: May 19 2019 03:12 PM
  • Joined: 24 Nov 2008
emmanuel d...

i find that with tooltip...it is working...

i tried to change the code....the double press do not work as below...

#singleinstance force
DoubleClickTime := DllCall("GetDoubleClickTime")   ; Get the doubleclicktime in milliseconds
$Space::                               
   KeyWait,Space                             
   if (A_TimeSinceThisHotkey>DoubleClickTime)                 
      Send,q{Tab}                
   else if (A_ThisHotkey = A_PriorHotkey && A_TimeSincePriorHotkey < DoubleClickTime)
      Send,w{Tab}
   else 
      Send,e{Tab}
   Return
...????????????

emmanuel d
  • Members
  • 519 posts
  • Last active: Jul 15 2017 12:04 PM
  • Joined: 29 Jan 2009
it wors as it schould.
doublepress = 2 X Singlepress
where the firstpres is send the e tab is send
when you press again, the second press is cached and wtab is send
so if you do not wich this behavior you need to use the first example in the first post.

this meens that even for a singlepress, you need to wait like 500ms to get result, like:
#singleinstance force
DoubleClickTime := DllCall("GetDoubleClickTime")   ; Get the doubleclicktime in milliseconds
return
$Space::                                    ; Space Supports SingleClick, DoubleClick and ClickAndHold
   KeyWait,Space                              ; wait until first keypres is ended
   if (A_TimeSinceThisHotkey>DoubleClickTime) {   ; if the hotkey is longer pressed than the time to doubleclick
      Send,q{Tab}                     ; Do some action
      return                              ; No need to do something else, so return
   }
   KeyWait, Space,% "d T"DoubleClickTime/1000         ; Wait for a new pres of space, x nr of seconds
   If ! Errorlevel                           ; If no error returned there was a doublepress or click
      Send,w{Tab}                    ; Than do some action
   else Send,e{Tab}                     ; If there was a timeout(error) means, no double , but singleclick, so do a action
   Return                                 ; We are donne so, return

Stopwatch emdkplayer
the code i post falls under the: WTFYW-WTFPL license

http://www.ahkscript.org/ the new forum


leehong1
  • Validating
  • 403 posts
  • Last active: May 19 2019 03:12 PM
  • Joined: 24 Nov 2008
emmanuel d....
i'll use the example in your first post....thank you again

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
see similar ideas here.