Notification of Copying Data Into the Clipboard Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Alexander2
Posts: 348
Joined: 27 Apr 2019, 17:38

Notification of Copying Data Into the Clipboard

12 May 2021, 08:37

Can someone explain why the following code does not work? It is designed to make sure that data is copied into the clipboard when the user presses Ctrl + C. Is there a more efficient way to write the code?

Code: Select all

^c::
Clipboard=
Send ^c
clipwait, 2, 1
if ErrorLevel=0
{
ToolTip %Clipboard%
sleep 400
tooltip
}
else
{
msgbox 0, NOTHING WAS COPIED, NOTHING WAS COPIED, 1
SoundBeep 100, 1000
ExitApp
}
return
User avatar
mikeyww
Posts: 28870
Joined: 09 Sep 2014, 18:38

Re: Notification of Copying Data Into the Clipboard

12 May 2021, 09:40

I'm not sure why you would need a script to copy text to the clipboard, but when you want to send the same key as the hotkey, precede the hotkey with $.

Explained: Dollar

OnClipboardChange can help you detect clipboard changes automatically. It's handy when you want an action to occur whenever the clipboard is changed.
Alexander2
Posts: 348
Joined: 27 Apr 2019, 17:38

Re: Notification of Copying Data Into the Clipboard

13 May 2021, 07:08

I'm not sure why you would need a script to copy text to the clipboard, but when you want to send the same key as the hotkey, precede the hotkey with $.
Thank you. I had included the Send ^C command in the code because the ^C:: hotkey disables the C key on the clipboard.

I can see now that the OnClipboardChange command is more efficient than the code which I had written.

Is the following code written efficiently to notify me whenever the clipboard contents change and thus make sure that data is copied into the clipboard when Ctrl + C is pressed?

Code: Select all

OnClipboardChange("clipchanged")
return
clipchanged() {
ToolTip %Clipboard%
sleep 400
tooltip
}
User avatar
mikeyww
Posts: 28870
Joined: 09 Sep 2014, 18:38

Re: Notification of Copying Data Into the Clipboard  Topic is solved

13 May 2021, 07:14

Yes, but the script is designed to exit unless you start it with #Persistent.

Example #1
Alexander2
Posts: 348
Joined: 27 Apr 2019, 17:38

Re: Notification of Copying Data Into the Clipboard

13 May 2021, 07:21

Is the command #Persistent redundant if my script (within which the OnClipboardChange command is nested) already never stops (because all the threads have the Return command at the end)?
User avatar
mikeyww
Posts: 28870
Joined: 09 Sep 2014, 18:38

Re: Notification of Copying Data Into the Clipboard

13 May 2021, 08:09

The existence of Return does not, in itself, make a script persistent. A demonstration is below.

Code: Select all

Gosub, Test
Return
Test:
Return
There are two answers to your question.

First:
A script is persistent if any of the following conditions are true:
At least one hotkey or hotstring has been defined in the script or created by the Hotkey command or Hotstring function, even if it is not enabled.
The keyboard hook or mouse hook is installed.
The script contains any use of Gui, even if it has not been called.
The script contains any use of OnMessage, or has called it dynamically or retrieved a reference with Func.
The Input command has been called.
The #Persistent directive is present anywhere in the script.
Second: if the script persists without the use of #Persistent, then you don't need it!
Alexander2
Posts: 348
Joined: 27 Apr 2019, 17:38

Re: Notification of Copying Data Into the Clipboard

13 May 2021, 08:40

Thank you for the explanation. Since my scrip to which I want to add the OnClipboardChange command contains many hotkeys and already persists, it is not necessary to add the #Persistent command.
Alexander2
Posts: 348
Joined: 27 Apr 2019, 17:38

Re: Notification of Copying Data Into the Clipboard

14 May 2021, 09:21

I have pasted the following code into a larger script (which has very many hotkey commands), but this code does not work when it is within that larger script:

Code: Select all

OnClipboardChange("clipchanged")
return
clipchanged() {
ToolTip %Clipboard%
sleep 400
tooltip
}
However, when I run this code in the following script which is smaller, the code works:

Code: Select all

OnClipboardChange("clipchanged")
return
clipchanged() {
ToolTip %Clipboard%
sleep 400
tooltip
}

Escape::
SoundBeep 100, 1000
ExitApp
return
Do you know what may be the reason why the code does not work in the larger script?
Alexander2
Posts: 348
Joined: 27 Apr 2019, 17:38

Re: Notification of Copying Data Into the Clipboard

14 May 2021, 09:29

I have now found the reason why the code does not work within the larger script: The code is not at the beginning of the script. When I put the code at the beginning of the script, it works.
Why does the code have to be put at the beginning of the larger script in order to work?
User avatar
mikeyww
Posts: 28870
Joined: 09 Sep 2014, 18:38

Re: Notification of Copying Data Into the Clipboard

14 May 2021, 09:40

You don't need the whole script or even the function to occur at the top, but you need the function call (first line) to be in the auto-execute section at the top. It otherwise would not execute unless part of a separate routine that executes, because that is how AutoHotkey works.
Alexander2
Posts: 348
Joined: 27 Apr 2019, 17:38

Re: Notification of Copying Data Into the Clipboard

15 May 2021, 07:49

Thank you. Now I understand that certain commands are to be put at the beginning of a code so that the program may load them into memory.
Can you also explain whether there is any difference between the terms "code" and "script"? Does the term "code" refer to the entire text of an .ahk file from top to bottom, whereas the term "script"—to a specific and smaller series of commands within the larger text?
User avatar
mikeyww
Posts: 28870
Joined: 09 Sep 2014, 18:38

Re: Notification of Copying Data Into the Clipboard

15 May 2021, 07:59

Good question! I find that people on the forum sometimes do use the terms differently, and probably not correctly (i.e., according to established and accepted usage) in many instances. I will give you my take on it. Others can chime in if they disagree.

Code is a general term to refer to anything that you write in a programming language. It's code rather than a code, like stuff is stuff, rather than a stuff.

A script is all of the code in your file, together. It's the whole thing.

A routine or subroutine is typically one section that has a label, such as a hotkey label, hotstring label, or other label followed by ":". The term routine could also be used more generally, such as to refer to a loop or other section of code. Subroutine is an established term from other languages based on how sections might be called with a Gosub or Call sort of command. The notion of a subroutine is that it is a labeled block of code that can be executed, after which the script is returned to its previous sequence of commands.

Function is also frequently misused as a term. A function is something specific in the syntax, as described in the documentation. It is perhaps a special kind of subroutine that accepts parameters and can return a value or object. AHK has many built-in functions. You can also create your own in any script. The notion of function, as in mathematics, is that you have inputs, and you have an output. The output is a function of the input, and that is why it is called a function.

I believe that this description is consistent with the AHK documentation.
Alexander2
Posts: 348
Joined: 27 Apr 2019, 17:38

Re: Notification of Copying Data Into the Clipboard

16 May 2021, 12:49

mikeyww wrote:
15 May 2021, 07:59
Good question! I find that people on the forum sometimes do use the terms differently, and probably not correctly (i.e., according to established and accepted usage) in many instances. I will give you my take on it. Others can chime in if they disagree.

Code is a general term to refer to anything that you write in a programming language. It's code rather than a code, like stuff is stuff, rather than a stuff.

A script is all of the code in your file, together. It's the whole thing.

A routine or subroutine is typically one section that has a label, such as a hotkey label, hotstring label, or other label followed by ":". The term routine could also be used more generally, such as to refer to a loop or other section of code. Subroutine is an established term from other languages based on how sections might be called with a Gosub or Call sort of command. The notion of a subroutine is that it is a labeled block of code that can be executed, after which the script is returned to its previous sequence of commands.

Function is also frequently misused as a term. A function is something specific in the syntax, as described in the documentation. It is perhaps a special kind of subroutine that accepts parameters and can return a value or object. AHK has many built-in functions. You can also create your own in any script. The notion of function, as in mathematics, is that you have inputs, and you have an output. The output is a function of the input, and that is why it is called a function.

I believe that this description is consistent with the AHK documentation.
Thank you for the explanations. Especially because English is not my native language, I did not know that the term “code” is uncountable when it is used in the context of programming. So I guess that an excerpt from a written program can be referred to as “a piece of code” or “a segment of code.”
I can see now that the code of an entire written program is called a script and that the code below a specific hotkey or within a loop is called a routine or a subroutine.
User avatar
mikeyww
Posts: 28870
Joined: 09 Sep 2014, 18:38

Re: Notification of Copying Data Into the Clipboard

16 May 2021, 12:58

I agree with you. The AHK documentation is helpful with some of the terms. Others are used more broadly in computer programming.
A script is simply a plain text file with the .ahk filename extension containing instructions for the program, like a configuration file, but much more powerful.
Macro is another term seen in the documentation.
A macro is a series of scripted actions that is "played" upon demand.
This is perhaps synonymous with "hotkey or hotstring subroutine", because "macro" has especially been used more broadly & historically, to refer to multi-command subroutines that are triggered by a keyboard sequence of some kind.
FCALIZZ
Posts: 7
Joined: 23 Jun 2023, 12:17

Re: Notification of Copying Data Into the Clipboard

27 Jun 2023, 11:08

hi, i have a similar question in which i would appreciate your assistance. I'm trying to make a script that pops up a tooltip saying "COPIED" if you copied a content by pressing Ctrl + C , or displays "CUTTED" if you actually cutted the content by pressing Ctrl + X. The script is not working as expected since it always displays "COPIED" even if i press the ctrl + X shortcut where it should be displaying "CUTTED" instead.

Code: Select all

MsgBox "this script will show a tooltip everytime the clipboard is modified, it will let you know if contents are copied or cutted"

#Persistent
OnClipboardChange("funcClipboardChange")
return

funcClipboardChange() {
	sizeClipboard:=StrLen(Clipboard)
    if (GetKeyState("Ctrl", "C"))
        ToolTip COPIED `n size: %sizeClipboard% chars approx `n contents: %Clipboard%
    else if (GetKeyState("Ctrl", "X"))
        ToolTip ---SCISSORED CUTTED--- `n size: %sizeClipboard% chars approx `n contents: %Clipboard%
    else
        ToolTip captured `n size: %sizeClipboard% chars approx `n contents: %Clipboard%

    Sleep 2000 ;wait 2 seconds
    ToolTip ; destroy tooltip.
}

User avatar
mikeyww
Posts: 28870
Joined: 09 Sep 2014, 18:38

Re: Notification of Copying Data Into the Clipboard

27 Jun 2023, 11:51

You cannot actually make up your own syntax. You have to use what AHK provides.

Code: Select all

#Requires AutoHotkey v1.1.33
~^c::tip("Copied")
~^x::tip("Cut")

tip(str) {
 ToolTip % str
 SetTimer tipOff, -2000
}

tipOff() {
 ToolTip
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 158 guests