Code Monkey home page Code Monkey logo

altv-os-keybinds's Introduction

Open Source - Keybinds

Created by LeonMrBonnie

❤️ Support me by becoming a Patron
⭐ This repository if you found it useful!

Generic badge


Description

This repository provides an alt:V resource to create keybinds.

This resource provides a few easy functions to work with keybinds.

Installing Dependencies / Installation

I cannot stress this enough. Ensure you have NodeJS 13+ or you will have problems.

  • NodeJS 13+
  • An Existing or New Gamemode
  • General Scripting Knowledge

After simply add the name of this resource to your server.cfg resource section.

altv-os-keybinds

Then simply clone this repository into your main server resources folder.

cd resources
git clone https://github.com/LeonMrBonnie/altv-os-keybinds

Ensure your package.json includes this property:

"type": "module"

How To Use

This resource is fully clientside. It has no serverside code.
To use this resource, you have to add it to the deps array in the resource.cfg of the resource, where you want to use the keybinds.
Then you can import the resource by using import * as Keybinds from "altv-os-keybinds"

Available functions

registerKeybind: (Arguments starting with a ? are optional)

Argument Description Type
key Key name (E.g. 'E') String
hold Does the key need to be held Boolean
debounce Debounce time for this keybind Number
onRelease Executed on key release Function
?onPress (Only for hold = true) Executed on key press Function

Returns: Keybind id

Example:

import * as Keybinds from "altv-os-keybinds";

// Register a keybind for the key E
let myKeybind = Keybinds.registerKeybind("E", true, 500, 
() => {
    alt.log(`Released key E`);
}, 
() => {
    alt.log(`Pressed key E`);
});

unregisterKeybind: (Arguments starting with a ? are optional)

Argument Description Type
keybindId Keybind id (Returned by registerKeybind) Number

Returns: Nothing

Example:

import * as Keybinds from "altv-os-keybinds";

let myKeybind = Keybinds.registerKeybind("E", false, 0, () => {
    Keybinds.unregisterKeybind(myKeybind); // Remove the keybind after the first press
});

isKeybindHeld: (Arguments starting with a ? are optional)

Argument Description Type
keybindId Keybind id (Returned by registerKeybind) Number

Returns: Boolean indicating if the key is currently held

Notice: This only works for keybinds with hold = true.

Example:

import * as alt from "alt-client";
import * as Keybinds from "altv-os-keybinds";

let myKeybind = Keybinds.registerKeybind("E", true, 0, () => {}, () => {});

// Only allow the player to move when the E key is held
alt.everyTick(() => {
    let gameControlsActive = alt.gameControlsEnabled();
    if(Keybinds.isKeybindHeld(myKeybind) && !gameControlsActive) alt.toggleGameControls(true);
    else if(gameControlsActive) alt.toggleGameControls(false);
});

getKeybindsFromKey: (Arguments starting with a ? are optional)

Argument Description Type
key Key name (E.g. 'E') String

Returns: Array containing all keybind ids with the specified key

Example:

import * as alt from "alt-client";
import * as Keybinds from "altv-os-keybinds";

Keybinds.registerKeybind("E", true, 0, () => {}, () => {});
Keybinds.registerKeybind("E", true, 0, () => {}, () => {});
Keybinds.registerKeybind("E", true, 0, () => {}, () => {});
Keybinds.registerKeybind("E", true, 0, () => {}, () => {});
Keybinds.registerKeybind("E", true, 0, () => {}, () => {});

let keybinds = Keybinds.getKeyBindsFromKey("E"); // Array containing the 5 keybind ids

setKeybindDisabled: (Arguments starting with a ? are optional)

Argument Description Type
keybindId Keybind id (Returned from registerKeybind) Number
disabled Should the keybind be disabled Boolean

Returns: Nothing

Example:

import * as alt from "alt-client";
import * as Keybinds from "altv-os-keybinds";

let keybind = Keybinds.registerKeybind("E", true, 0, () => {}, () => {});

Keybinds.setKeybindDisabled(keybind, true); // Disables the keybind

isKeybindDisabled: (Arguments starting with a ? are optional)

Argument Description Type
keybindId Keybind id (Returned from registerKeybind) Number

Returns: Boolean indicating if the keybind is disabled

Example:

import * as alt from "alt-client";
import * as Keybinds from "altv-os-keybinds";

let keybind = Keybinds.registerKeybind("E", true, 0, () => {}, () => {});

alt.log(Keybinds.isKeybindDisabled(keybind)); // Logs 'false'
Keybinds.setKeybindDisabled(keybind, true); // Disables the keybind
alt.log(Keybinds.isKeybindDisabled(keybind)); // Logs 'true'

setKeyDisabled: (Arguments starting with a ? are optional)

Argument Description Type
key Key name (E.g. 'E') Number
disabled Should the key be disabled Boolean

Returns: Nothing

Example:

import * as alt from "alt-client";
import * as Keybinds from "altv-os-keybinds";

Keybinds.registerKeybind("E", true, 0, () => {}, () => {});
Keybinds.registerKeybind("E", true, 0, () => {}, () => {});
Keybinds.registerKeybind("E", true, 0, () => {}, () => {});
Keybinds.registerKeybind("E", true, 0, () => {}, () => {});
Keybinds.registerKeybind("E", true, 0, () => {}, () => {});

Keybinds.setKeyDisabled("E", true); // Disables all the 5 created keybinds

Other alt:V Open Source Resources

altv-os-keybinds's People

Contributors

leonmrbonnie avatar

Stargazers

FabOne avatar

Watchers

 avatar

Forkers

water-flow

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.