Jump to content

_isPressed() with control keys


Recommended Posts

So my code is supposed to behave as follows, I highlight some text and press ctrl+space, the script copies the selected text and goes and gets information from a bunch of files on my local machine and comes back with some useful information. At that point it displays a small gui with a single label displaying that information. The gui should stay up until I release the control key. This is what I have

... snip

    $lineLen = StringLen($line)
    
    $mouse = MouseGetPos()
    
    $gui = GUICreate("Definition", $lineLen*7, 50, 0, $mouse[1])
    GUISetState(@SW_SHOW)
    $label = GUICtrlCreateLabel($line, 10, 25, ($lineLen*7), 30)
    While(True)
        Sleep(100)
        GUICtrlSetData($label, _IsPressed('11'))  ; for debugging
        If (_IsPressed('11') == 0) Then
            ExitLoop
        EndIf
    WEnd
       
       msgbox(0, "pause", "don't kill the gui just yet so that we can see the result of _isPressed"); debugging 

    GUIDelete($gui)

This works fine except for one really weird thing. When I do left control + space it behaves exactly as I would expect, it quickly pops up a gui with the information and stays up until I've released left-control. But when I do right-control + space it brings up the gui but the gui stays up no matter what. I release all the keys on the keyboard and it stays up. It's only after I press in the left-control and release IT that the gui closes.

Anybody have any ideas?

*edit: added code tags

Edited by Traddles
Link to comment
Share on other sites

So my code is supposed to behave as follows, I highlight some text and press ctrl+space, the script copies the selected text and goes and gets information from a bunch of files on my local machine and comes back with some useful information. At that point it displays a small gui with a single label displaying that information. The gui should stay up until I release the control key. This is what I have

... snip
 
     $lineLen = StringLen($line)
     
     $mouse = MouseGetPos()
     
     $gui = GUICreate("Definition", $lineLen*7, 50, 0, $mouse[1])
     GUISetState(@SW_SHOW)
     $label = GUICtrlCreateLabel($line, 10, 25, ($lineLen*7), 30)
     While(True)
         Sleep(100)
         GUICtrlSetData($label, _IsPressed('11')) ; for debugging
         If (_IsPressed('11') == 0) Then
             ExitLoop
         EndIf
     WEnd
        
        msgbox(0, "pause", "don't kill the gui just yet so that we can see the result of _isPressed"); debugging    
 
     GUIDelete($gui)

This works fine except for one really weird thing. When I do left control + space it behaves exactly as I would expect, it quickly pops up a gui with the information and stays up until I've released left-control. But when I do right-control + space it brings up the gui but the gui stays up no matter what. I release all the keys on the keyboard and it stays up. It's only after I press in the left-control and release IT that the gui closes.

Anybody have any ideas?

*edit: added code tags

It would be a lot easier for people to help if you could post some code which we could run and which is intended to show the problem ^_^

Because I'm not sure what your code is doing, especially the missing code, I tried this, and it works for me whichever Ctrl key I use

#include <misc.au3>
$line = "abcdef"
$lineLen = StringLen($line)

$mouse = MouseGetPos()

$gui = GUICreate("Definition", $lineLen * 7 + 50, 50, 0, $mouse[1])
GUISetState(@SW_SHOW)
$label = GUICtrlCreateLabel($line, 10, 25, ($lineLen * 10), 30)

While (True)
    Sleep(100)

    If _IsPressed('11') Then
        GUICtrlSetData($label, "Ctrl down"); for debugging
        $labelsettppressed = True
       ;do something
        While _IsPressed('11')
            Sleep(50)
        WEnd
        GUICtrlSetData($label, "Ctr up") 
        ExitLoop
        
    EndIf
WEnd

MsgBox(0, "pause", "don't kill the gui just yet so that we can see the result of _isPressed"); debugging
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Ok, no problem. Here's a snipped down version of the complete working program, if you run this and try using ctrl+space using both control keys you should see what I'm talking about

#include <WinAPI.au3>

HotKeySet("^{SPACE}", "LookUpWord")

While (True)
    Sleep(100)
WEnd

Func LookUpWord()

    $clipBuff = ClipGet()
    Send("^c")
    Sleep(10)
    $word = " "&ClipGet()&" "
    
    ClipPut($clipBuff)
    
    If ($word == "  " Or $word == " " & $clipBuff & " ") Then
        Return
    EndIf
    
; snip bunch of stuff that opens files and stuff to get an actual definition for the word that was just coppied
    $line = "some definition for that word that was just copied into the clipboard"
    
    $lineLen = StringLen($line)
    
    $mouse = MouseGetPos()
    
    $gui = GUICreate("Definition", $lineLen*7, 50, 0, $mouse[1])
    GUISetState(@SW_SHOW)
    $label = GUICtrlCreateLabel($line, 10, 25, ($lineLen*7), 30)
    While(True)
        Sleep(100)
        If (_IsPressed('11') == 0) Then
            ExitLoop
        EndIf
    WEnd
    
    GUIDelete($gui)
    
EndFunc
Link to comment
Share on other sites

Try this.

#include <WinAPI.au3>
#include <Misc.au3>

HotKeySet("^{SPACE}", "LookUpWord")

While (True)
    Sleep(100)
WEnd

Func LookUpWord()

    $clipBuff = ClipGet()
;    Send("^c")
    _keybd_event(0x43, 0x00)
    Sleep(100)
    _keybd_event(0x43, 0x02)
    $word = " "&ClipGet()&" "
    
    ClipPut($clipBuff)
    
    If ($word == "  " Or $word == " " & $clipBuff & " ") Then
        Return
    EndIf
    
; snip bunch of stuff that opens files and stuff to get an actucal definition for the word that was just coppied
    $line = "some definition for that word that was just copied into the clipboard"
    
    $lineLen = StringLen($line)
    
    $mouse = MouseGetPos()
    
    $gui = GUICreate("Definition", $lineLen*7, 50, 0, $mouse[1])
    GUISetState(@SW_SHOW)
    $label = GUICtrlCreateLabel($line, 10, 25, ($lineLen*7), 30)
    While(True)
        Sleep(10)
        If (_IsPressed('11') == 0) Then
            ExitLoop
        EndIf
    WEnd
    
    GUIDelete($gui)
    
EndFunc

func _keybd_event($vkCode, $Flag)
    DllCall('user32.dll', 'int', 'keybd_event', 'int', $vkCode, 'int', 0, 'int', $Flag, 'ptr', 0)
endfunc; _keybd_event
Link to comment
Share on other sites

Ok, that worked.... but why? I'm an engineer and have a habit of not liking to run things that I don't understand. What is this dll call doing?

DllCall('user32.dll', 'int', 'keybd_event', 'int', $vkCode, 'int', 0, 'int', $Flag, 'ptr', 0)

and how does changing how I get the selected text fix my gui loop?

*edit, forgot the code tags again

Edited by Traddles
Link to comment
Share on other sites

It appears that the [Ctrl] key simulated press in the Send() call is never registered as being "up" again. _IsPressed("A2") returns 1 even when you're using the right control key to trigger the script. It will depend on how the underlying C++ code is implementing the functionality for Send(). Even the target application (SciTE in my case) thinks the left control key is down until you toggle it yourself by pressing it.

Yashied's example uses a deprecated call (see http://msdn.microsoft.com/en-us/library/ms...04(VS.85).aspx) to synthesise the keystroke. Because you've already got [Ctrl] held down, it just sends a keydown/keyup event for the "c" key which effectively sends [Ctrl]C to the application.

Hope that makes more sense for you.

WBD

Link to comment
Share on other sites

I see, so basically doing Send("^c") doesn't actually send a "keyUp" signal? That seems bad to me. Although it does explain my problems.

It turns out that the Send("^c") does not work properly until RCTRL is hold down. I do not know why this is happening. If you do not fit my trick, then look for this forum way to copy to the clipboard without calling Send() function.

Good luck.

Link to comment
Share on other sites

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...