assign a Keyboard Shortcut to a User Button

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
atisoro
Posts: 29
Joined: 27 Jan 2025 16:27

Re: assign a Keyboard Shortcut to a User Button

Post by atisoro »

can you help by an example?

highend
Posts: 13990
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: assign a Keyboard Shortcut to a User Button

Post by highend »

Show the full script in code tags
One of my scripts helped you out? Please donate via Paypal

atisoro
Posts: 29
Joined: 27 Jan 2025 16:27

Re: assign a Keyboard Shortcut to a User Button

Post by atisoro »

it's too hard for me.
I stop here

atisoro
Posts: 29
Joined: 27 Jan 2025 16:27

Re: assign a Keyboard Shortcut to a User Button

Post by atisoro »

it doesn't matter.
I got hold of the solution.
(with regexreplace)
It works, but it does not take into account the existence of any previously set buttons.

Code: Select all

 $newCB = <<<'EOD'
[CustomButtons]
Version=1
Count=1
1=""loader|:ladybug|1|0"  load "<xyscripts>.\myscript.xys";		"

[BookmarkButtons]  
 EOD;

 $content = readfile("<xydata>\<xyini>");
 $content_new = regexreplace($content, "\[CustomButtons]([\s\S]*?)\[BookmarkButtons]", $newCB);
 writefile("<xydata>\<xyini>", $content_new, "o");
 #190;   //restart
any addition (such as by code addition of CommandButton1 in the toolbar) is welcome

highend
Posts: 13990
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: assign a Keyboard Shortcut to a User Button

Post by highend »

toolbar()?
One of my scripts helped you out? Please donate via Paypal

atisoro
Posts: 29
Joined: 27 Jan 2025 16:27

Re: assign a Keyboard Shortcut to a User Button

Post by atisoro »

smoothly add a custom button at the end of active toolbar

Code: Select all

$oldtoolbar = toolbar();
 toolbar($oldtoolbar."-,ctb1,".);

highend
Posts: 13990
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: assign a Keyboard Shortcut to a User Button

Post by highend »

Can you at least test such things more extensively before posting them?

You're effectively removing the last entry with it^^

Code: Select all

    $oldToolbar = toolbar();
    if (!regexmatches($oldToolbar, "\bctb1\b")) {
        toolbar($oldToolbar . ",ctb1");
    }
One of my scripts helped you out? Please donate via Paypal

atisoro
Posts: 29
Joined: 27 Jan 2025 16:27

Re: assign a Keyboard Shortcut to a User Button

Post by atisoro »

actually, I initially tested this code to adds to the first position and I mistakenly assumed that it works correctly (surprisingly, it doesn't)
working code //for first position cbt1

Code: Select all

$oldtoolbar = toolbar();
 toolbar("ctb1,-".$oldtoolbar); 
my proposals are only at the beginner level, (a few days of juggling the codes), my experience being in VBA and VB.net
in any case, I accept the criticism

highend
Posts: 13990
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: assign a Keyboard Shortcut to a User Button

Post by highend »

toolbar("ctb1,-".$oldtoolbar);

Cool, and now you're removing the existing first entry^^
One of my scripts helped you out? Please donate via Paypal

atisoro
Posts: 29
Joined: 27 Jan 2025 16:27

Re: assign a Keyboard Shortcut to a User Button

Post by atisoro »

indeed
your code works flawlessly
Kudos

Code: Select all

    $oldToolbar = toolbar();
    if (!regexmatches($oldToolbar, "\bctb1\b")) {
        toolbar($oldToolbar . ",ctb1");
    }

highend
Posts: 13990
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: assign a Keyboard Shortcut to a User Button

Post by highend »

And this is a way how it could be really done...

Code: Select all

    $script = <<<'#EOD'
"loader|:ladybug|1|0" load "<xyscripts>\myscript.xys";
#EOD;

    $script     = replace(trim($script, <crlf>), <crlf>, "¶");
    $iniFile    = "<xydata>\<xyini>";
    $iniContent = readfile($iniFile);

    $section = getsectionlist("CustomButtons", $iniFile);
    $cntBtns = gettoken(regexmatches($section, "^Count=\d+$"), 2, "=");
    $section = regexreplace($section, "^(Version|Count)=\d+(\r?\n|$)");

    $btnID = "";
    while ($i++ < 64) {
        $line = gettoken($section, $i, <crlf>);
        // Find an existing but not-populated line or an empty one
        if (regexmatches($line, "^\d{1,2}=""""$") || $line == "") {
            $btnID = $i;
            break;
        }
    }
    end ($btnID == ""), "No free (x of 64) custom button entry found, aborted!";

    // Add new / overwrite empty entry and adjust the "Count" if necessary
    $newLine = quote($script . <tab 2>);
    if ($btnID > $cntBtns) { setkey $btnID, "Count", "CustomButtons", $iniFile, 2; }
    setkey $newLine, $btnID, "CustomButtons", $iniFile, 2;

    // Restart without saving
    #190;
One of my scripts helped you out? Please donate via Paypal

atisoro
Posts: 29
Joined: 27 Jan 2025 16:27

Re: assign a Keyboard Shortcut to a User Button

Post by atisoro »

this is sophisticated far beyond the knowledge of most users (at least mine).
elegant, simple and useful.

atisoro
Posts: 29
Joined: 27 Jan 2025 16:27

Re: assign a Keyboard Shortcut to a User Button

Post by atisoro »

again, a problem in which I ask for your help.
after the code successfully writes to the xyplorer.ini file,
Image
whatever I try, after Exit(saving) or savesettings or save configuration, the changes disappear.
Image
can anything be done?
Attachments
t2.png
t2.png (3.75 KiB) Viewed 302 times
t1.png
t1.png (5.91 KiB) Viewed 302 times

highend
Posts: 13990
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: assign a Keyboard Shortcut to a User Button

Post by highend »

That's why it uses "Restart without saving" at the end^^
One of my scripts helped you out? Please donate via Paypal

atisoro
Posts: 29
Joined: 27 Jan 2025 16:27

Re: assign a Keyboard Shortcut to a User Button

Post by atisoro »

This trick works great.
Thank you.

Post Reply