xtermdemo.lpr 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. program xtermdemo;
  2. {$mode objfpc}
  3. uses
  4. browserapp, JS, Classes, SysUtils, Web, xterm;
  5. type
  6. { TMyApplication }
  7. TMyApplication = class(TBrowserApplication)
  8. FTerminal : TXTerm.TTerminal;
  9. FTermEl : TJSHTMLElement;
  10. Finp : TJSHTMLInputElement;
  11. FBtnSend : TJSHTMLButtonElement;
  12. procedure doRun; override;
  13. private
  14. function DoData(data: String): Boolean;
  15. function DoKey(key : TXTerm.TOnKeyCallbackDataType): Boolean;
  16. function DoSendClick(aEvent: TJSMouseEvent): boolean;
  17. end;
  18. function TMyApplication.DoKey(key : TXTerm.TOnKeyCallbackDataType): Boolean;
  19. Var
  20. printable : Boolean;
  21. begin
  22. Result:=true;
  23. With key do
  24. printable :=Not (domEvent.altKey or domEvent.metaKey or domEvent.ctrlKey or domEvent.metaKey);
  25. if key.domEvent.Key=TJSKeyNames.BackSpace then
  26. FTerminal.write(#8' '#8)
  27. else if key.domEvent.Key=TJSKeyNames.Enter then
  28. FTerminal.writeln('')
  29. else if Printable then
  30. FTerminal.write(Key.Key)
  31. end;
  32. function TMyApplication.DoData(data: String): Boolean;
  33. begin
  34. Result:=True;
  35. if Data=#8 then
  36. begin
  37. console.log('backspace detected');
  38. FTerminal.Write(data+' '+data)
  39. end
  40. else
  41. begin
  42. console.log('OnData : ',Data, (length(data)));
  43. FTerminal.Write(data)
  44. end;
  45. end;
  46. procedure TMyApplication.doRun;
  47. begin
  48. FTermEl:=GetHTMLElement('xterm');
  49. FInp:=TJSHTMLInputElement(GetHTMLElement('edtInput'));
  50. FBtnSend:=TJSHTMLButtonElement(GetHTMLElement('btnSend'));
  51. FBtnSend.OnClick:=@DoSendClick;
  52. FTerminal:=TXTerm.TTerminal.New;
  53. FTerminal.open(FTermEl);
  54. // FTerminal.OnData(@DoData);
  55. FTerminal.OnKey(@DoKey);
  56. Terminate;
  57. end;
  58. function TMyApplication.DoSendClick(aEvent: TJSMouseEvent): boolean;
  59. begin
  60. FTerminal.Writeln(Finp.Value);
  61. end;
  62. var
  63. Application : TMyApplication;
  64. begin
  65. Application:=TMyApplication.Create(nil);
  66. Application.Initialize;
  67. Application.Run;
  68. end.