How should I handle text input (to fit in with my fake text mode stuff)
-
I'm trying to decide how to approach text input in my fake text mode lib.
I've seen a few things like FlxInput and FlxUI however text input is going to be reflected or echoed via my text mode code. That is you type a letter it will output it to the 'fake text console'. How do I use FlxInput and is it only for input (like can i make sure it doesn't put anything on the screen so I can use my own display stuff -- or even simpler say you want to enter in a code invisibly -- remember GodMode wolfenstein 3d and doom?)
Essentially I want to handle most all of the keys and their respective symbols. Letters, numbers, punctuation, everything you type in using a shift key (for instance the punctuation above the numbers or uppercase letters), caps lock, and maybe even the old text alt-codes (alt+number = text character)
In any case can anyone help me out?
-
You should grab the key code of the last pressed key, and push it into an array of characters. Then check, for example, if the string is "idkfa", and you do your thing.
Here's some pseudo, not that pseudo code (untested)
var inputcode:Array<String> = new Array(); if (FlxG.keys.anyJustPressed) inputcode.push(FlxG.keys.getIsDown); then if(inputcode == "idkfa") custom code here
-
As much as I wanted a haxeflixel solution, I'm using the openfl get key down keyboard event and grabbing the character codes right from that... it's working pretty dang well too. here's some code:
// the only ones I'll allow to type typeableCharacters = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; // the actual input string to do with later input = ""; // position on the string itself (not screen position) inputPos = 0; // Add a keyboard event FlxG.stage.addEventListener( KeyboardEvent.KEY_DOWN, onKeyDown ); private function onKeyDown( evt : KeyboardEvent ) : Void { var char : String; char = String.fromCharCode( evt.charCode ); // make sure we can type this character, also check bounds if( ( typeableCharacters.indexOf( char ) > -1 ) && inputPos < consoleWidth - 1 ) { // a function which will return a new string with the inserted character input = InsertChar( input, char, inputPos ); inputPos++; } else if ( evt.keyCode == Keyboard.LEFT ) { // cursor control left inputPos--; if ( inputPos < 0 ) inputPos = 0; } else if ( evt.keyCode == Keyboard.RIGHT && inputPos < consoleWidth - 1 ) { // cursor control right inputPos++; if ( inputPos > input.length ) inputPos = input.length; } else if ( evt.keyCode == Keyboard.BACKSPACE ) { // function to delete char (in this backspace over char) input = DeleteChar( input, inputPos ); inputPos--; if ( inputPos < 0 ) inputPos = 0; } else if ( evt.keyCode == Keyboard.DELETE ) { // with the last parameter set to delete instead of backspace input = DeleteChar( input, inputPos, true ); } } // If the position is set to -1, which it is by default, we merely append the character // however if greater than, we need to actually build a new string with the character in it public function InsertChar( source : String, charToInsert : String, pos : Int = -1 ) : String { var result : String = ""; // If the position is past the end of the source string, then we just make sure it appends if( pos > source.length - 1 ) pos = -1; // this means we want to insert a character into the middle somewhere if ( pos > -1 ) { // copy up to the current position var firstPart : String = source.substring( 0, pos ); //add the new character to that firstPart += charToInsert; // then add the rest of the string onto the newly created string result = firstPart + source.substring( pos ); } else result = source + charToInsert; // just append return result; } public function DeleteChar( source : String, pos : Int = -1, delete : Bool = false ) : String { var result : String = ""; if( pos > source.length - 1 ) pos = -1; // this means backspace if ( delete == false ) { // if position specified if ( pos > -1 ) { // very simply copy from the start to the position subtracing a character var firstPart : String = source.substring( 0, pos - 1 ); // then add the rest of the string onto that one with the missing character thereby backspacing result = firstPart + source.substring( pos ); } else { // drop off a character result = source.substring( 0, source.length - 1 ); } } else { // this is the deletion code here we do the opposite of above // copy from 0 to the cursor position var firstPart : String = source.substring( 0, pos ); // when we add this second part we're starting one character in // effectively it deletes the character instead of backspacing result = firstPart + source.substring( pos + 1 ); } return result; }