Sign in to follow this  
Followers 0
orbs

detect when only the Ctrl key is pressed (not in combination)

5 posts in this topic

hello all,

i'm trying to detect a long click on left or right Ctrl. my purpose is to assign a custom command to it, so instead pressing some weird keyboard combinations, i'll just need to click one of the Ctrl keys for ~300ms, and my script will perform some action (which i'll define later, intention to switch keyboard layout and typing direction simultaneously, similar to some Linux distros, which i find very convenient).

i can pretty much detect the key press and the duration, with slight modification to the _IsPressed example:

#include <Misc.au3>

Local $hDLL = DllOpen("user32.dll")

While True
    If _IsPressed("A2", $hDLL) Then ; left Ctrl was pressed
        ConsoleWrite("_IsPressed - left Ctrl key was pressed." & @CRLF)
        ; Wait until key is released.
        $hTimer=TimerInit()
        While _IsPressed("A2", $hDLL)
            Sleep(100)
        WEnd
        $nTime=TimerDiff($hTimer)
        ConsoleWrite("_IsPressed - left Ctrl key was released after "&$nTime&' ms' & @CRLF)
    ElseIf _IsPressed("1B", $hDLL) Then
        MsgBox(0, "_IsPressed", "The Esc Key was pressed, therefore we will close the application.")
        ExitLoop
    EndIf
    Sleep(250)
WEnd

DllClose($hDLL)

but that does not exclude a combination click, like Ctrl+A.

how would i detect when Ctrl only was pressed and released?

or perhaps there is a completely different approach to this?

thanks for any suggestion.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hi.

#include <Misc.au3>

local $hDLL = DllOpen("user32.dll")
Local $begin = 0

Local $dif =0
While 1
      If _IsPressed(11) Then
        ConsoleWrite("CTRL was Pressed" & @crlf)
        While _IsPressed(11)
            ConsoleWrite("CTRL keep Pressed" & @crlf)
            Beep(1000, 100)
        WEnd
        ConsoleWrite("CTRL was  Release" & @crlf)
     EndIf



    Sleep(100)
WEnd

saludos

Edited by Danyfirex

Share this post


Link to post
Share on other sites

Danyfirex,

That script does practically the same thing as his script, except you look for any CTRL key instead of just the left one. Which is not what he's looking for.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I believe the OP is asking how to distinguish between pressing just CTRL, and CTRL used in a combination. You script works when CTRL is struck, but I can still use CTRL + A in combination.

Edit: Too slow :)

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

I got his answer wrong. sorry.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0