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.");
    }
});


Messages In This Thread
simple toggle macro - by lumenant - 08-12-2025, 03:59 PM
RE: simple toggle macro - by Gintaras - 08-12-2025, 06:26 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)