Jump to content
Sign in to follow this  

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

Recommended Posts

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.
        While _IsPressed("A2", $hDLL)
        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.")


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


#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)
        ConsoleWrite("CTRL was  Release" & @crlf)



Edited by Danyfirex

Share this post

Link to post
Share on other sites


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

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

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...