3
3
* SPDX-License-Identifier: Apache-2.0
4
4
**********************************************************************/
5
5
import { Component , Input , OnInit , Output , EventEmitter } from '@angular/core'
6
- import { C , V } from '@angular/cdk/keycodes'
7
6
import { Terminal } from '@xterm/xterm'
8
7
@Component ( {
9
8
selector : 'amt-terminal' ,
@@ -15,44 +14,42 @@ export class TerminalComponent implements OnInit {
15
14
@Output ( ) handleKeyPress : EventEmitter < any > = new EventEmitter < any > ( )
16
15
container ! : HTMLElement | null
17
16
ngOnInit ( ) : void {
17
+ console . log ( 'Terminal component ngOnInit' )
18
+ if ( this . term == null ) throw new Error ( 'Terminal not found' )
18
19
this . container = document . getElementById ( 'terminal' )
19
20
if ( this . container == null ) throw new Error ( 'Container not found' )
20
21
this . term . open ( this . container )
21
- this . term . onData ( ( data : any ) => {
22
- this . handleKeyPress . emit ( data )
23
- } )
24
- this . term . attachCustomKeyEventHandler ( ( e : KeyboardEvent ) : boolean => {
25
- e . stopPropagation ( )
26
- // Due to a new 'HACK' in xtermjs, calling e.preventDefault() here
27
- // results in all upper case characters being dropped,
28
- // so only call it if we 'consume' the keydown here.
29
- // Note: this function can be called for 'keypress' and 'keyup' events as well as 'keydown' events
30
- if ( e . type === 'keydown' ) {
31
- if ( e . ctrlKey && e . shiftKey && e . keyCode === C ) {
32
- e . preventDefault ( )
33
- navigator . clipboard
34
- . writeText ( this . term . getSelection ( ) )
35
- . then ( ( ) => { } )
36
- . catch ( ( err ) => {
37
- console . error ( 'Failed to copy to clipboard' , err )
38
- } )
39
- } else if ( e . ctrlKey && e . shiftKey && e . keyCode === V ) {
40
- e . preventDefault ( )
22
+ this . term . onKey ( ( { key, domEvent } ) => {
23
+ console . log ( 'Key event onKey:' , domEvent )
24
+ domEvent . preventDefault ( )
25
+ if ( domEvent . ctrlKey && domEvent . key === 'c' ) {
26
+ if ( this . term . hasSelection ( ) ) {
41
27
navigator . clipboard
42
- . readText ( )
43
- . then ( ( text ) => {
44
- this . handleKeyPress . emit ( text )
45
- } )
46
- . catch ( ( err ) => {
47
- console . error ( 'Failed to read clipboard' , err )
48
- } )
49
- } else if ( e . code === 'Space' ) {
50
- // or e.keyCode === SPACE
51
- e . preventDefault ( )
52
- this . handleKeyPress . emit ( e . key )
28
+ . writeText ( this . term . getSelection ( ) )
29
+ . then ( ( ) => { } )
30
+ . catch ( ( err ) => {
31
+ console . error ( 'Failed to copy to clipboard' , err )
32
+ } )
33
+ } else {
34
+ console . log ( 'Sending Ctrl + C (Interrupt Signal)' ) ;
35
+ // Send interrupt signal (SIGINT) to terminal
36
+ this . term . write ( '\x03' ) // Sends Ctrl + C (ASCII code 03)
53
37
}
38
+ // Prevent the default browser behavior (like opening menus)
39
+ return false
40
+ }
41
+ if ( domEvent . ctrlKey && domEvent . key === 'v' ) {
42
+ navigator . clipboard
43
+ . readText ( )
44
+ . then ( ( text ) => {
45
+ this . handleKeyPress . emit ( text )
46
+ } )
47
+ . catch ( ( err ) => {
48
+ console . error ( 'Failed to read clipboard' , err )
49
+ } )
50
+ return false
54
51
}
55
- return false
52
+ this . handleKeyPress . emit ( key )
56
53
} )
57
54
}
58
55
}
0 commit comments