Input.hx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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.scale(2);
  11. debug.x = debug.y = 5;
  12. input = new h2d.TextInput(font, s2d);
  13. input.backgroundColor = 0x80808080;
  14. // input.inputWidth = 100;
  15. input.text = "Click to édit";
  16. input.textColor = 0xAAAAAA;
  17. input.scale(2);
  18. input.x = input.y = 50;
  19. input.onFocus = function(_) {
  20. input.textColor = 0xFFFFFF;
  21. }
  22. input.onFocusLost = function(_) {
  23. input.textColor = 0xAAAAAA;
  24. }
  25. input.onChange = function() {
  26. while( input.text.length > 20 )
  27. input.text = input.text.substr(0, -1);
  28. }
  29. }
  30. function getKeyName(id) {
  31. var name = hxd.Key.getKeyName(id);
  32. if( name == null ) name = "#"+id;
  33. return name;
  34. }
  35. override function update(dt:Float) {
  36. // check special keys state
  37. debug.text = "Cursor: " + input.cursorIndex + ", Sel: " + input.getSelectedText()+", Touch screen: " + hxd.System.getValue(IsTouch) + ", Down: "+[for( i in 0...1024 ) if( hxd.Key.isDown(i) ) getKeyName(i)].join(",");
  38. }
  39. static function main() {
  40. new Input();
  41. }
  42. }