So this was a quick project made for mouse and keyboard functionality in future projects. Hopefully someone out there will find it useful for them as well! Below is a "Features" list which also serves as a guide that explains how it works and how to utilize it, as well as some notes about particulars. Enjoy!
|Keyboard and Mouse state monitoring through the use of _keyboard() and _mouse() functions in the update step.*See below.|
|Currently Pressed Keyboard Key is stored in _kbk.|
|A keyboard string is stored in _kbs and sent to the "terminal," or _kbt, when return/enter is pressed. Afterwhich kbs is cleared.|
|Pressing backspace properly deletes the last character of _kbs.|
|Mouse Button state is stored in _mb.|
|Mouse X and Y are stored in _mx and _my. (If you would like to check if a sprite or similar is clicked on, just add a function that is called when (_mblu==true AND _mx >= _ox AND _mx<=_ox+_ow AND _my >= _oy AND _my<=_oy+_oh) where _ox, _oy, _ow, _oh are the to be clicked object's x, y, width, and height respectively.)|
|Mouse Button being held down is stored in _mbl, _mbr, and _mbm for left, right, and middle mouse buttons respectively. If their value >0 then they are being held down.|
|Most of the time you will probably just be checking if the button is being held down or has been released up. These can be checked with _mblu and _mbld for up and down respectively. (This goes for each button: _mbru, _mbrd, _mbmu, _mbmd; following the same pattern.) These are boolean values that simply become true on the frame they happen and are reset to false every frame.|
|Underscores before all variables and functions used by the library to prevent any naming conflicts with existing projects. (You can remove these if you want to save on tokens so long as it doesn't conflict with existing variable names.)|
|Check out the _demo() function's code for an example of implementing the library.|
A few notes:
Pressing p while typing brings up the pico-8 menu. (As does Enter in the browser.) So keep that in mind while you're working.
This thing isn't browser friendly at all, the demo at least. But Pico-8 just isn't there yet with browser compatibility and mouse/keyboard support.
I tried to keep the token AND character count as low as I possible could. I'm sure it could be optimized more. You guys are nuts with that stuff, but I did the best I could to make it tiny and unobtrusive. The library itself sits at 178 tokens and 693 characters.
Note there's no handling for keyboard keys that aren't string characters, backspace, or enter. So things like escape, delete, etc.. they are still added to _kbs but just display as a blank space when printed. But these keys can be easily handled through _kbk the same as return and backspace if you want.
Feature request is permitted. However, I don't want to add too much "specific" functionality. I want to keep it lightweight enough so that it can be slotted into pretty much any project that needs mouse and keyboard support with only minor editing based on case use.
Feel free to let me know if I messed anything up or could have optimized things more in terms of speed or token count!
Feel free to use freely in your games! Credit to MKLIB would be appreciated if you use it, though not necessary!
Why does the demo say "LEFT UP!" "RIGHT UP!" "MIDDLE UP!" before I even press anything?*See below.
- "Why isn't X,Y,Z feature in? This isn't very much code!" It is meant to be a minimalistic library that can easily be expanded upon but contains the bare-bones necessities to alleviate writing these same lines over and over again. It also is intended to help people who may not know how to approach using the mouse/keyboard with pico-8.
*These functions must be called BEFORE any function that tries to read any of their variables because in order to save tokens I do not predefine these variables in the _init() except for kbs which was necessary. If you wanted to you could initialize all of the variables in your _init() code and then it wouldn't matter if they got called early and it would also remove the first 3 "Key up!" messages you see displayed at the start of the demo.
[Please log in to post a comment]