Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
simple toggle macro
#1
Big Grin 
    im trying to create a code that when i press a key, i did 't' in this case, as a placeholder, it will continuously press it 100 times per second until i press the key again, a toggle. this is what i wrote so far, just based off my limited knowledge of coding C# but i am very new to this program and so i dont know any intricacies of this program that are not present in normal C#. there are 2 errors i got which are here and after is my code:  Smile

errors:

Compilation: 2 errors
Script1.cs(10,6): error CS0117: 'keys' does not contain a definition for 'onKeyDown'
Script1.cs(25,9): error CS0165: Use of unassigned local variable 'cts'

script:

// Toggle spam of "T" key at 100 presses per second in LibreAutomate
// Press T once to start, press T again to stop.

using System;
using System.Threading;

bool spamming = false;
CancellationTokenSource cts;

keys.onKeyDown(KKey.T, k => {
    if (!spamming) {
        // Start spamming
        spamming = true;
        cts = new CancellationTokenSource();
        Task.Run(() => {
            while (!cts.Token.IsCancellationRequested) {
                keys.send("T");
                Thread.Sleep(10); // 10 ms delay = ~100 presses/sec
            }
        });
        print.it("T spam started.");
    } else {
        // Stop spamming
        spamming = false;
        cts.Cancel();
        print.it("T spam stopped.");
    }
});
#2
Currently AI have not enough knowledge about LibreAutomate to create working code. I tried to ask this question too, and got another non-working code.

But when I gave it relevant LibreAutomation documentation articles, AI created this code (just forgot to assign false/null to the variables, I fixed it). And it works. I used OpenAI model `gpt-5` via API.
 
Code:
Copy      Help
bool sending=false;
Thread? worker=null;

var at = new Au.Triggers.ActionTriggers();

at.Hotkey["T"] = o => {
    sending = !sending;
    print.it(sending ? "T spam ON" : "T spam OFF");
    if (sending && worker is null) {
        worker = run.thread(() => {
            while (sending) {
                keys.more.sendKey(KKey.T); // press and release T
                10.ms(); // ~100 per second (see note below)
            }
            worker = null;
        });
    }
};


at.Hotkey["Esc"] = o => script.end();

at.Run();

I would do it differently, but this works too.

You probably will want to move that code to your `Hotkey triggers` file. Then need to modify it slightly. Code to paste in file `Hotkey triggers`:
 
Code:
Copy      Help
        bool sending = false;
        Thread worker = null;
        hk["F9"] = o => {
            sending = !sending;
            print.it(sending ? "T spam ON" : "T spam OFF");
            if (sending && worker is null) {
                worker = run.thread(() => {
                    try {
                        while (sending) {
                            keys.more.sendKey(KKey.T); // press and release T
                            10.ms(); // ~100 per second (see note below)
                        }
                    }

                    catch (Exception) { sending = false; }
                    worker = null;
                });
            }
        };

Here I changed the hotkey to F9.

Most of this code could be moved to a separate script.


Forum Jump:


Users browsing this thread: 1 Guest(s)