Author Topic: [Resource] Keybind Functions - AddKeyBind, RemoveKeyBind  (Read 2062 times)

I always thought that there needed to be a default function to do this, but there isn't, so here you go:



Code: (addKeyBind) [Select]
function addKeyBind(%div,%name,%cmd,%device,%action,%overWrite)
{
if(%device !$= "" && %action !$= "")
{
if(moveMap.getCommand(%device,%action) $= "" || %overWrite)
moveMap.bind(%device,%action,%cmd);
}

%divIndex = -1;
for(%i=0; %i < $RemapCount; %i++)
{
if($RemapDivision[%i] $= %div)
{
%divIndex = %i;
break;
}
}

if(%divIndex >= 0)
{
for(%i=$RemapCount-1; %i > %divIndex; %i--)
{
$RemapDivision[%i+1] = $RemapDivision[%i];
$RemapName[%i+1] = $RemapName[%i];
$RemapCmd[%i+1] = $RemapCmd[%i];
}

$RemapDivision[%divIndex+1] = "";
$RemapName[%divIndex+1] = %name;
$RemapCmd[%divIndex+1] = %cmd;
$RemapCount ++;
}
else
{
$RemapDivision[$RemapCount] = %div;
$RemapName[$RemapCount] = %name;
$RemapCmd[$RemapCount] = %cmd;
$RemapCount ++;
}
}
This adds a keybind, and if desired can forcibly or non-forcibly bind it to a key.

Code: (EXAMPLE) [Select]
addKeyBind("Division","Name","command","keyboard","alt f",0);Division - If the division used already exists, the bind will be added under the existing division.
Name - The name of the bind.
Command - The command called when this bind is used. The function called has one variable for the state of the bound key (bool up/down).
Device - This is not needed for normal use! This is used when setting the bind to a key. (keyboard/mouse)
Action - This is not needed for normal use! This is used when setting the bind to a key. Set this to an action such as "shift G" or "alt v".
Overwrite - This is not needed for normal use! This is used when setting the bind to a key. If this is true, any existing binding using the specified device/action will be overwritten. (bool)




Code: (removeKeyBind) [Select]
function removeKeyBind(%div,%name,%cmd)
{
%binding = moveMap.getBinding(%cmd);
%device = getField(%binding,0);
%action = getField(%binding,1);

if(%device !$= "" && %action !$= "")
moveMap.unBind(%device,%action);

for(%i=0; %i < $RemapCount; %i++)
{
%d = $RemapDivision[%i];
%n = $RemapName[%i];
%c = $RemapCmd[%i];

%start = 0;

if(%n $= %name && %c $= %cmd && (%d $= %div || %lastDiv $= %div))
{
%start = %i + 1;
break;
}

if(%d !$= "")
%lastDiv = %d;
}

if(%start > 0)
{
for(%i=%start; %i < $RemapCount; %i++)
{
$RemapDivision[%i-1] = $RemapDivision[%i];
$RemapName[%i-1] = $RemapName[%i];
$RemapCmd[%i-1] = $RemapCmd[%i];
}

%d = $RemapDivision[%start-1];
if(%d $= "")
$RemapDivision[%start-1] = %div;

$RemapDivision[$RemapCount-1] = "";
$RemapName[$RemapCount-1] = "";
$RemapCmd[$RemapCount-1] = "";

$RemapCount --;
}
}
This removes an existing keybind properly.

Code: (EXAMPLE) [Select]
removeKeyBind("Division","Name","command");Division - This division will remain if there are still other binds under it.
Name - The name of the bind.
Command - The command called when this bind is used.



Hope this helps people!

Why not just do this?
Code: [Select]
$remapDivision[$remapCount] = "Devision";
$remapName[$remapCount] = "Name";
$remapCmd[$remapCount] = "Function Called";
$remapCount++;

Why not just do this?
Code: [Select]
$remapDivision[$remapCount] = "Devision";
$remapName[$remapCount] = "Name";
$remapCmd[$remapCount] = "Function Called";
$remapCount++;
because with his code you can just do this:
Code: [Select]
addKeyBind("Division","Name","command","keyboard","alt f",0);

Why not just do this?
Code: [Select]
$remapDivision[$remapCount] = "Devision";
$remapName[$remapCount] = "Name";
$remapCmd[$remapCount] = "Function Called";
$remapCount++;

So you can place a keybind in an already existing division

Also this method screws up servercommandgui, which to be honest could be a heck of a lot more flexible.
I dunno if I can be bothered to fix it, but I might find myself with a whole bunch of free time and revamp some of my old stuff


Which method?

The entire OP

It inserts the keybind into a spot and bumps everything down, but I got kinda lazy with servercommandgui and just set a variable to remember where the division is so it can be popped in easily.  Especially since the DRPG client exec's first, then adds keybinds later (really gay imo), everything after WILL get bumped, making it a sad day for servercommandgui.

You could just use these functions for it.

You could just use these functions for it.

I remember seeing this when I made servercommandgui, but then there was some specific reason that I could not use it.  I forget what, and I may have since edited the mod so it doesn't matter.

i remember ephi had something similar to this, i ripped it out of the macro saver or something for my addons
although i don't think ephi's could overwrite or set a keybind, so this is probably more useful overall