0
!c::
    file_name = footnote.ini                             
    restore_original_clipBoard := clipboard
    clipboard =
    KeyWait, Alt                
    KeyWait, c                                           ;small c
    BlockInput, on
    SendEvent, ^{ins}                                   ;^c doesn't work
    ClipWait, 2                                     ; Wait for the clipboard to contain text.
    if ErrorLevel
    {
        MsgBox Failed to save the selection: %clipboard%
        exit
    }
    BlockInput, off
    save_selection := clipboard 

Problem: Despite a selection being made, Sendevent ^{ins} does not save it to the clipboard. Sometimes I have to repeat my hotkey, alt + c several times before the selection is being copied to the clipboard. The KeyWait should ensure me that only ^{ins} is being processed without any additional keys. What am I doing wrong here?


UPDATE One of the ways I tried to force copy a selection to the clipboard was by using a while loop. I got it to work through the post: Looping clipboard and errorlevel evaluation not working as expected

PROBLEM When I make a selection and press alt + c it sometimes gets stuck in the infinite loop that I implemented. But as you can see from that code:

clipboard := ""
while( StrLen(clipboard) < 1 )
{
    Send, ^{ins}
    Sleep, 50
}
MsgBox % ClipBoard

The infinite loop incorporates within itself a continues resending of ^{ins}. For some reason, my selection is not being recognized as a selection. Whilst it is in that infinite loop, I try to reselect the text. It then recognizes it instantly and copies my selection to the clipboard. But alas! The selection is incomplete because it goes so quick. This problem is not always like that. Sometimes it recognizes the selection first spot on! So sometimes it copies my selection to my clipboard sometimes not. When it does not, then a resending of a ^{ins} does not seem to work. I do not want to the user to reselect his selection. Is that possible to do?

Community
  • 1
  • 1
Khalil
  • 311
  • 5
  • 16
  • 1
    Khalil, are you aware that you can and should accept or discuss answers to your questions? You have asked several questions over the last few days and accepted none of the answers. – MCL Aug 11 '13 at 20:40

4 Answers4

1
Send {Ctrl Down}{c}{Ctrl Up}

That presses Ctrl+C, you must do it instantly as one command apposed to pressing Ctrl waiting then pressing C.

Never seen Insert key used for copying text.

Also found this sends Ctrl+C as well.

Send, ^c

To send insert key use

{Insert}
SSpoke
  • 5,656
  • 10
  • 72
  • 124
  • It doesn't work. There are whole threads dedicated to the issue of ^c not working for counts of people. Your workaround doesn't do anything at all. Also, {ins} is a correct abbreviation for {insert}. – Khalil Aug 11 '13 at 20:40
1

This way works for me:

!vk43:: ; alt+c
   clipContent:=ClipboardAll
   Clipboard:=""
   SendEvent, ^{Ins}
   ClipWait, .75
   MsgBox, % 262 . (ErrorLevel ? 160:208)
         , % ErrorLevel ? "Period expired:":"Result:"
         , % ErrorLevel ? "Failed to save the selection.":Clipboard
         , % (ErrorLevel ? 0:2) . .5
   Clipboard:=clipContent
   KeyWait, vk43
   Return
Grey
  • 339
  • 1
  • 4
  • I am trying figure out what this is doing, but the documentation is not forthcoming. Can you please clarify for me the MsgBox part? Why do you put a % in front of 262 followed by a dot? What does 160:208 mean? What does 0:2 mean? then another . followed by 0.5 seconds? – Khalil Aug 14 '13 at 13:58
  • Also, in case it failed, I want it to retry copying until it works. – Khalil Aug 14 '13 at 13:58
  • **Khalil** : _Can you please clarify for me the MsgBox part?_ I gave explanation in the code below. **Khalil** : _I want it to retry copying until it works._ See in the code below. – Grey Aug 15 '13 at 01:34
  • **Khalil**: _Also, can you explain why !vk43:: works better than !c:: ?_ Because the keybord layout is not effect on these hotkeys. I [mean](http://stackoverflow.com/a/16594926) the keys that change with the input language (if languages a several in the system). – Grey Aug 15 '13 at 01:35
  • Thanks for the link to the benefit of virtual scan codes! I see that I can now use the hotkey even if I changed keyboard layout. – Khalil Aug 15 '13 at 14:57
1
!vk43:: ; alt+c
   clipContent:=ClipboardAll ; backup clipboard content (if needed)
   Clipboard:="" ; no comment :)
   Loop
   {
      SendEvent, ^{Ins}
      ClipWait, .75 ; means 750ms, same if write 0.75
      ; assign value of "ErrorLevel" an variable for further usage
      errLvl:=ErrorLevel
      ; monitoring current action (for debugging purpose only)
      TrayTip, % "attempt: #"A_Index
             , % """ErrorLevel"" of ""ClipWait"" command is: "errLvl
   }
   ; here you can set the condition of ending the cycle: either...
   ; ...variable errLvl has not a true value,...
   ; ...or the number of attempts is equal 5
   Until, Not errLvl Or A_Index=5
   ; value of each field of the command "MsgBox"...
   ; ...are set depending on the value of errLvl variable...
   ; ...using a ternary expression
   ; means if errLvl is a true, "options" field is 262160
   MsgBox, % 262 . (errLvl ? 160:208)
         ; means that "title" has a couple variants
         , % errLvl ? "Period expired:":"Result:"
         ; means that "text" has a couple variants
         , % errLvl ? "Failed to save the selection.":Clipboard
         ; means if errLvl is a true, "timeout" field is 0.5 (500ms)
         , % (errLvl ? 0:2) . .5
/* same that and above:
   IfEqual, errLvl, % True, MsgBox, 262160
                                  , % "Period expired:"
                                  , % "Failed to save the selection."
                                  , 0.5
   Else MsgBox, 262208, % "Result:", % Clipboard, 2.5
*/
   TrayTip ; remove "TrayTip" (for debugging purpose only)
   ; save an positive result (if needed)
   IfEqual, errLvl, 0, Sleep, -1, someVar:=Clipboard
   ; return a temporarily saved data into clipboard (if needed)
   Clipboard:=clipContent
   KeyWait, % "vk43"
   Return
Grey
  • 339
  • 1
  • 4
1

From my experience whenever keystrokes are not recognized reliably it's due to either the system or the targeted program not keeping up with the speed at which those keys are sent.

For SendEvent you could try something like SetKeyDelay, 1000, 1000 and see if this improves things. The other option would be to send explicit down and up keys with intermittent sleep calls as outlined in this answer.

Community
  • 1
  • 1
MayaS
  • 21
  • 1