Author Topic: Creating Keybinds that activate console codes?  (Read 683 times)

Hello, earlier I was messing around with scripts and I started thinking if there was anyway to type a slash command and activate a console code?
For example: Lets say I type "/Fire", is there anyway to make it so "/Fire" will activate mousefire(1); in the console? Thanks.
« Last Edit: March 24, 2011, 06:53:17 PM by FrozenEye »

you should stay away from making client-sided commands (for this purpose). keybinds would be better for this.

you should stay away from making client-sided commands (for this purpose). keybinds would be better for this.
Well, that was my original idea, but when I studied other addons with keybinds I couldnt get anything out of them. Well, I was able to bind a key to it, but the key was virtually worthless. If anyone could help me with that I would be thankful.

It would have to be client sided, and I personally am against using slash commands for client sided code.

You could however, use another symbol, like ^fire, where ^ calls client sided commands.

Code: [Select]

package ClientCmds
{
function NMH_Type::Send(%this)
{
if(getSubStr(%this.getValue(),0,1) $= "^")
{
%this.setValue(getSubStr(%this.getValue(),1,strLen(%this.getValue())-1));
%str = %this.getValue();
%cmd = getWord(%str,0);
for(%x=1;%x<getWordCount(%str);%x++)
{
%args = %args @ getWord(%str,%x);
if(%x+1 != getWordCount(%str))
%args = %args @ ",";
}
%this.setValue("");
if(%args !$= "")
echo("call(ClientChatCmd" @ %cmd @ "," @ %args @ ");");
else
call("ClientChatCmd" @ %cmd);
}
Parent::send(%this);
}
};
activatePackage(ClientCmds);

Put this into a separate script file. Call it ClientChatCmds.cs
Now in your client.cs, at the first line, put exec("./ClientChatCmds.cs");

And to make your command, fire, you would do this:

Code: [Select]
function ClientChatCmdFire(%val)
{
mousefire(%val);
}

And then type ^fire 1 in game to start mousefire, ^fire 0 to stop it.

you should stay away from making client-sided commands (for this purpose). keybinds would be better for this.
But with this way you could add args, like so:

Code: [Select]
function ClientChatCmdHelloWorld(%arg1, %arg2)
{
echo("Hello world!");
if(%arg1 !$= "")
echo(%arg1);
if(%arg2 !$= "")
echo(%arg2);
}

Thanks, that helped a lot. I didnt expect something like this so quickly!
But anyway, sense i'm still trying to figure things like this out, how would you do a keybind that would do the same exact thing? Thanks :D

As for keybinds:

first make this into AddBind.cs. Use this exact file for any other keybind mods you make.
Code: [Select]
function AddBind(%division, %name, %command)
{
for(%i=0;%i<$remapCount;%i++)
{
if($remapDivision[%i] $= %division)
{
%foundDiv = 1;
continue;
}
if(%foundDiv && $remapDivision[%i] !$= "")
{
%position = %i;
break;
}
}
if(!%foundDiv)
{
error("Division not found: " @ %division);
return;
}
if(!%position)
{
$remapName[$remapCount] = %name;
$remapCmd[$remapCount] = %command;
$remapCount++;
return;
}
for(%i=$remapCount;%i>%position;%i--)
{
$remapDivision[%i] = $remapDivision[%i - 1];
$remapName[%i] = $remapName[%i - 1];
$remapCmd[%i] = $remapCmd[%i - 1];
}
$remapDivision[%position] = "";
$remapName[%position] = %name;
$remapCmd[%position] = %command;
$remapCount++;
}

Then in your client.cs
Code: [Select]
exec("./AddBind.cs");

function holdMouseFire(%val) //Each keybind needs it's own function
{ //%val is to check if the button was pressed or released
if(%val) //Only do the following code if the button was pressed
{
$holdFire = $holdFire ? 0:1; //This will swap $holdFire between 0 and 1 every time this line happens. Very quick toggle.
mousefire($holdFire);
}
}

package holdFire
{
function mouseFire(%val) //We need this package so you can't normally click to interupt the holdfire.
{
if($holdFire && !%val) // If holdfire is on, and you are releasing the button, don't do anything
return;
else
Parent::mouseFire(%val);
}
};
activatePackage(holdFire);

//This is the part where you add the keybind
//You'll need a different $global variable for different add-ons. such as $addedHoldCrouchKeyBinds
if(!$addedHoldFireKeyBinds)
{
AddBind("ToggleKeys","Hold MouseFire","holdMouseFire");
$addedHoldFireKeyBinds = true;
}


This is untested though. It might give a syntax error.
Comments about the code are after //s,  you can leave them in or take them out. Be sure to read them.

I really appreciate all the help you've given me, Chrono. But sense i'm trying to learn, could you help me with a few things?

As for keybinds:

first make this into AddBind.cs. Use this exact file for any other keybind mods you make.
function AddBind(%division, %name, %command) Now, here would I change these to what I want them to be?
{
   for(%i=0;%i<$remapCount;%i++)
   {
      if($remapDivision[%i] $= %division)
      {
         %foundDiv = 1;
         continue;
      }
      if(%foundDiv && $remapDivision[%i] !$= "")
      {
         %position = %i;
         break;
      }
   }
   if(!%foundDiv)
   {
      error("Division not found: " @ %division);
      return;
   }
   if(!%position)
   {
      $remapName[$remapCount] = %name;
      $remapCmd[$remapCount] = %command;
      $remapCount++;
      return;
   }
   for(%i=$remapCount;%i>%position;%i--)
   {
      $remapDivision[%i] = $remapDivision[%i - 1];
      $remapName[%i] = $remapName[%i - 1];
      $remapCmd[%i] = $remapCmd[%i - 1];
   }
   $remapDivision[%position] = "";
   $remapName[%position] = %name;
   $remapCmd[%position] = %command;
   $remapCount++;
}


Then in your client.cs

exec("./AddBind.cs");

function holdMouseFire(%val) //Each keybind needs it's own function
{            //%val is to check if the button was pressed or released
   if(%val)   //Only do the following code if the button was pressed - What exactly do you mean here?
   {
      $holdFire = $holdFire ? 0:1; //This will swap $holdFire between 0 and 1 every time this line happens. Very quick toggle.
      mousefire($holdFire);
   }
}

package holdFire
{
   function mouseFire(%val)   //We need this package so you can't normally click to interupt the holdfire.
   {
      if($holdFire && !%val) // If holdfire is on, and you are releasing the button, don't do anything
         return;
      else
         Parent::mouseFire(%val);
   }
};
activatePackage(holdFire);

//This is the part where you add the keybind
//You'll need a different $global variable for different add-ons. such as $addedHoldCrouchKeyBinds
if(!$addedHoldFireKeyBinds)
{
   AddBind("ToggleKeys","Hold MouseFire","holdMouseFire"); -Would I choose one of these, or could I add a Activate/Deactivate?
   $addedHoldFireKeyBinds = true;
}


This is untested though. It might give a syntax error.
Comments about the code are after //s,  you can leave them in or take them out. Be sure to read them.
That is it for now. And I tried to make the questions clear, but you might have to think about what im saying. Also, sorry for it being hard to find the questions, I didnt know you couldnt Bold code

I do not advise using this command solely, without all the extra settings like Chrono gave, but this is what I use for one of my commands I use in my server:

Code: [Select]
GlobalActionMap.bindCmd(keyboard, "F6", "", "commandToServer('radiusimpulse');");

Chrono, the code that you gave me doesnt work, ive tried fiddling with it, but it still dosnt work