2
0

Input.hx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. class Input extends hxd.App {
  2. var input : h2d.TextInput;
  3. var debug : h2d.Text;
  4. override function init() {
  5. engine.backgroundColor = 0x202020;
  6. var font = hxd.res.DefaultFont.get();
  7. var console = new h2d.Console(font, s2d);
  8. console.addCommand("hello", "Prints the correct answer", [], function() console.log("World", 0xFF00FF));
  9. debug = new h2d.Text(font, s2d);
  10. debug.x = debug.y = 5;
  11. input = new h2d.TextInput(font, s2d);
  12. input.backgroundColor = 0x80808080;
  13. // input.inputWidth = 100;
  14. input.text = "Click to édit";
  15. input.textColor = 0xAAAAAA;
  16. input.scale(2);
  17. input.x = input.y = 50;
  18. input.onFocus = function(_) {
  19. input.textColor = 0xFFFFFF;
  20. }
  21. input.onFocusLost = function(_) {
  22. input.textColor = 0xAAAAAA;
  23. }
  24. input.onChange = function() {
  25. while( input.text.length > 20 )
  26. input.text = input.text.substr(0, -1);
  27. }
  28. }
  29. function getKeyName(id) {
  30. var name = hxd.Key.getKeyName(id);
  31. if( name == null ) name = "#"+id;
  32. return name;
  33. }
  34. override function update(dt:Float) {
  35. // check special keys state
  36. debug.text = "Cursor: " + input.cursorIndex + ", Sel: " + input.getSelectedText()+", Down: "+[for( i in 0...1024 ) if( hxd.Key.isDown(i) ) getKeyName(i)].join(",");
  37. }
  38. static function main() {
  39. new Input();
  40. }
  41. }