autohotkeyautohotkey-2

Unable to empty clipboard in AutoHotKey v2.0.18


So, I was trying to update a script using the Clipboard of Windows 11. The script was nothing much; just a hotstring. It inserts a string of characters, with the SendText command. However, I decided Clipboard was better as it was more reliable, and was much faster. The main reason is a certain "keydelay" issue. Sometimes, AHK would kinda "lag" and wait for my input before inserting any more characters from the given string. So, I decided Clipboard would be kind of like a Get Out of Jail for Free card, avoiding the annoying issue completely. However, Clipboard turned out to be more of a roadblock than ever; But this time, it is probably an actual bug, either with the system or with AutoHotKey.

The code snippet I used for testing:-

#Requires AutoHotkey v2.0
#SingleInstance

!,::Clipboard := ""

What it's supposed to do is clear the entire clipboard, when I press Alt + ,. The hotkey part doesn't matter much; only the action it's supposed to execute does.

I would expect Clipboard := "" to empty the clipboard. I had used it for a test. I was trying to see if it actually empties the Windows clipboard. But, believe it or not, it never did. I had also tried Clipboard= and A_Clipboard := "" but in vain.

So, what could be the issue here? Thanks.

Edit:- The issue seems to be with my system, it doesn't support it. Is there any alternative to it?

Update:- I understood the function wrong. Clipboard := "" prevents Ctrl+V from sending the latest entry. The real question is How do you clear parts of the clipboard history?

Update 2:- See my answer given below, I found the solution to what I need by myself.
PS the Clipboard and A_Clipboard variables reference the Clipboard. What I really needed was a way to delete an entry from the clipboard history. Needed to get the wording right


Solution

  • The built-in variable "Clipboard" of AHK v1 is replaced in AHK v2 by A_Clipboard.

    #Requires AutoHotkey v2.0
    #SingleInstance
    
    !,:: A_Clipboard := ""
    

    The 'A_' prefix indicates that the variable is a built-in Variable.

    Try it first in a script with no other code and close all other scripts for the case that something else interferes.

    EDIT:

    To delete the first entry from the legacy clipboard, use this:

    #Requires AutoHotkey v2.0
    #SingleInstance
    
    !,:: Run "cmd.exe" " /c`"echo off | clip"
    

    To delete all the data in the legacy clipboard, create a batch file, add this code to it

    %windir%\System32\cmd.exe /c "echo off | clip"
    wmic service where "name like '%%cbdhsvc_%%'" call stopservice
    wmic service where "name like '%%cbdhsvc_%%'" call startservice
    

    and run it manuell or use the run command to run it from your script.

    For details see https://www.windowscentral.com/how-clear-clipboard-data-shortcut-windows-10