TextEditor >> keyStroke:
keyStroke: anEvent
 	self resetTypeAhead.

	(self dispatchOnKeyboardEvent: anEvent) 
		ifTrue: [
			self closeTypeIn.
			self storeSelectionInParagraph.
			^self].

	self openTypeIn.
	self 
		zapSelectionWith: self typeAhead contents; 
		resetTypeAhead;
		unselect;
		storeSelectionInParagraph.

TextEditor class >> initializeCmdKeyShortcuts
initializeCmdKeyShortcuts
	"Initialize the (unshifted) command-key (or alt-key) shortcut table."

	"NOTE: if you don't know what your keyboard generates, use Sensor kbdTest"

	"TextEditor initialize"

	| cmdMap cmds |
	cmdMap := Array new: 256 withAll: #noop:.		"use temp in case of a crash"
	cmdMap at: 1 + 1 put: #cursorHome:.				"home key"
	cmdMap at: 4 + 1 put: #cursorEnd:.				"end key"
	cmdMap at: 8 + 1 put: #backspace:.				"ctrl-H or delete key"
	cmdMap at: 11 + 1 put: #cursorPageUp:.			"page up key"
	cmdMap at: 12 + 1 put: #cursorPageDown:.		"page down key"
	cmdMap at: 13 + 1 put: #crWithIndent:.			"cmd-Return"
	cmdMap at: 27 + 1 put: #offerMenuFromEsc:.		"escape key"
	cmdMap at: 28 + 1 put: #cursorLeft:.				"left arrow key"
	cmdMap at: 29 + 1 put: #cursorRight:.				"right arrow key"
	cmdMap at: 30 + 1 put: #cursorUp:.				"up arrow key"
	cmdMap at: 31 + 1 put: #cursorDown:.				"down arrow key"
	cmdMap at: 32 + 1 put: #selectWord:.				"space bar key"
	cmdMap at: 127 + 1 put: #forwardDelete:.		"del key"
			
	'0123456789' 
		do: [:char | cmdMap at: char asciiValue + 1 put: #changeEmphasis:].
	
	cmds := #($a #selectAll: $c #copySelection: $e #exchange: $f #find: $g #findAgain: $j #doAgain: $k #offerFontMenu: $u #align: $v #paste: $w #backWord: $x #cut: $y #swapChars: $z #undo:).
	1 to: cmds size
		by: 2
		do: [:i | cmdMap at: (cmds at: i) asciiValue + 1 put: (cmds at: i + 1)].
		
	cmdActions := cmdMap

TextEditor class >> initializeShiftCmdKeyShortcuts
initializeShiftCmdKeyShortcuts 
	"Initialize the shift-command-key (or control-key) shortcut table."
	"NOTE: if you don't know what your keyboard generates, use Sensor kbdTest"
	"wod 11/3/1998: Fix setting of cmdMap for shifted keys to actually use the 
	capitalized versions of the letters.
	TPR 2/18/99: add the plain ascii values back in for those VMs that don't return the shifted values."

	"TextEditor initialize"
	
	| cmdMap cmds |

	"shift-command and control shortcuts"
	cmdMap := Array new: 256 withAll: #noop:.  		"use temp in case of a crash"
	cmdMap at: ( 1 + 1) put: #cursorHome:.			"home key"
	cmdMap at: ( 4 + 1) put: #cursorEnd:.				"end key"
	cmdMap at: ( 8 + 1) put: #forwardDelete:.			"ctrl-H or delete key"
	cmdMap at: (11 + 1) put: #cursorPageUp:.			"page up key"
	cmdMap at: (12 + 1) put: #cursorPageDown:.		"page down key"
	cmdMap at: (13 + 1) put: #crWithIndent:.			"ctrl-Return"
	cmdMap at: (27 + 1) put: #offerMenuFromEsc:.	"escape key"
	cmdMap at: (28 + 1) put: #cursorLeft:.			"left arrow key"
	cmdMap at: (29 + 1) put: #cursorRight:.			"right arrow key"
	cmdMap at: (30 + 1) put: #cursorUp:.				"up arrow key"
	cmdMap at: (31 + 1) put: #cursorDown:.			"down arrow key"
	cmdMap at: (32 + 1) put: #selectWord:.			"space bar key"
	cmdMap at: (45 + 1) put: #changeEmphasis:.		"cmd-sh-minus"
	cmdMap at: (61 + 1) put: #changeEmphasis:.		"cmd-sh-plus"
	cmdMap at: (127 + 1) put: #forwardDelete:.		"del key"

	cmds := #(
		$c	compareToClipboard:
		$h	cursorTopHome:
		$j	doAgainUpToEnd:
		$k	changeStyle:
		$m	selectCurrentTypeIn:
		$s	findAgain:
		$u	changeLfToCr:
		$x	makeLowercase:
		$y	makeUppercase:
		$z	redo: "makeCapitalized:"
	).
	1 to: cmds size by: 2 do: [ :i |
		cmdMap at: ((cmds at: i) asciiValue + 1) put: (cmds at: i + 1).			"plain keys"
		cmdMap at: ((cmds at: i) asciiValue - 32 + 1) put: (cmds at: i + 1).		"shifted keys"
		cmdMap at: ((cmds at: i) asciiValue - 96 + 1) put: (cmds at: i + 1).		"ctrl keys"
	].
	shiftCmdActions := cmdMap

SmalltalkEditor class >> initializeCmdKeyShortcuts
initializeCmdKeyShortcuts
	"Initialize the (unshifted) command-key (or alt-key) shortcut table."
	"NOTE: if you don't know what your keyboard generates, use Sensor kbdTest"
	"SmalltalkEditor initialize"
	| cmds |
	super initializeCmdKeyShortcuts.
	cmds := #($b #browseIt: $d #doIt: $i #inspectIt: $l #cancel: $m #implementorsOfIt: $n #sendersOfIt: $o #spawnIt: $p #printIt: $q #querySymbol: $s #save: ).
	1 to: cmds size
		by: 2
		do: [ : i | cmdActions at: (cmds at: i) asciiValue + 1 put: (cmds at: i + 1)].
	"Set up type-method argument hot keys, 1-4.."
	'1234' do:
		[ : eachKeyboardChar |
		cmdActions 
			at: eachKeyboardChar asciiValue + 1
			put: #typeMethodArgument: ]

SmalltalkEditor class >> initializeShiftCmdKeyShortcuts
initializeShiftCmdKeyShortcuts 
	"Initialize the shift-command-key (or control-key) shortcut table."
	"NOTE: if you don't know what your keyboard generates, use Sensor kbdTest"
	| cmds |
	super initializeShiftCmdKeyShortcuts.
	cmds := #(
		$a	argAdvance:
		$b	browseItHere:
		$d	debugIt:
		$e	methodStringsContainingIt:
		$f	displayIfFalse:
		$g	fileItIn:
		$i	exploreIt:
		$n	referencesToIt:
		$s	invokePrettyPrint:
		$t	displayIfTrue:
		$v	pasteInitials:
		$w	methodNamesContainingIt:
	).
	1 to: cmds size by: 2 do: [ :i |
		shiftCmdActions at: ((cmds at: i) asciiValue + 1) put: (cmds at: i + 1).			"plain keys"
		shiftCmdActions at: ((cmds at: i) asciiValue - 32 + 1) put: (cmds at: i + 1).		"shifted keys"
		shiftCmdActions at: ((cmds at: i) asciiValue - 96 + 1) put: (cmds at: i + 1).		"ctrl keys"
	].
	"shift+cmd _ (underscore)"
	shiftCmdActions at: $_ asciiValue+1 put: #flattenSelection:

HandMorph >> interrupted
interrupted
	"Something went wrong - we're about to bring up a debugger. 
	Release some stuff that could be problematic."
	self releaseAllFoci. "or else debugger might not handle clicks"


EventSensor >> commandKeyPressed
commandKeyPressed
	"Answer whether the command key on the keyboard is being held down."

	^ self peekButtons anyMask: (2r1000 "cmd" bitShift: EventSensor numMouseButtons)

EventSensor >> installInterruptWatcher
installInterruptWatcher
	"Initialize the interrupt watcher process. Terminate the old process if any."
	"Sensor installInterruptWatcher"

	InterruptWatcherProcess ifNotNil: [InterruptWatcherProcess terminate].
	InterruptWatcherProcess := [self userInterruptWatcher] forkAt: Processor lowIOPriority.

EventSensor >> interruptWatcherProcess
interruptWatcherProcess
	"Answer my interrupt watcher process, if any"
	^InterruptWatcherProcess

EventSensor >> oldPrimInterruptSemaphore:
oldPrimInterruptSemaphore: aSemaphore 
	"Primitive. Install the argument as the semaphore to be signalled whenever the user presses the interrupt key. The semaphore will be signaled once each time the interrupt key is pressed."

	<primitive: 134>
	^self primitiveFailed
"Note: This primitive is obsolete with the new event driven architecture in which EventSensor can handle the interrupts itself. However, for supporting older images running on newer VMs the primitive must still be implemented."

EventSensor >> oldPrimSetInterruptKey:
oldPrimSetInterruptKey: anInteger
	"Primitive. Register the given keycode as the user interrupt key. The low byte of the keycode is the ISO character and its next four bits are the Smalltalk modifer bits <cmd><opt><ctrl><shift>."

	<primitive: 133>
	^self primitiveFailed
"Note: This primitive is obsolete with the new event driven architecture in which EventSensor can handle the interrupts itself. However, for supporting older images running on newer VMs the primitive must still be implemented."

EventSensor >> userInterruptWatcher
userInterruptWatcher
	"Wait for user interrupts and open a notifier on the active process when one occurs."
	[ interruptSemaphore wait.
	Display deferUpdates: false.
	SoundService stop.
	Smalltalk handleUserInterrupt ] repeat

EventSensor class >> installMappingToCommandKeys
installMappingToCommandKeys
	"Maps all keyboard events that arrive with the CONTROL modifier to also have the COMMAND modifier set. This mapping also considers the preference #mapControlCharactersToPrintableCharacters."

	| controlMask |
	0 "NUL" to: 27 "ESC" do: [:control |
		controlMask := self mapControlCharactersToPrintableCharacters
			ifTrue: [16r60] ifFalse: [0].
		KeyDecodeTable
			at: { control . 2r0010 "ctrl" }
			put: { control bitOr: controlMask . 2r1010 "cmd+ctrl" }.
		controlMask := self mapControlCharactersToPrintableCharacters
			ifTrue: [16r40] ifFalse: [0].
		KeyDecodeTable
			at: { control . 2r0011 "ctrl+shift" }
			put: { control bitOr: controlMask . 2r1011 "cmd+ctrl+shift" }].	

	28 "arrow keys" to: 126 "$~" do: [:arrowAndPrintable |
			KeyDecodeTable
				at: { arrowAndPrintable . 2r0010 "ctrl" }
				put: { arrowAndPrintable . 2r1010 "cmd+ctrl" }.
			KeyDecodeTable
				at: { arrowAndPrintable . 2r0011 "ctrl+shift" }
				put: { arrowAndPrintable . 2r1011 "cmd+ctrl+shift" }].
			
	self mapControlCharactersToPrintableCharacters ifFalse: [
		"See #fixControlCharacters. #linuxOnly"
		64 "$@" to: 95 "$_"do: [:upper |
			KeyDecodeTable
				at: { upper . 2r0011 "ctrl+shift" }
				put: { upper bitAnd: 16r9F . 2r1011 "cmd+ctrl+shift" }].
		96 "$`" to: 126 "$~" do: [:lower | "Ignore 127"
			KeyDecodeTable
				at: { lower . 2r0010 "ctrl" }
				put: { lower bitAnd: 16r9F . 2r1010 "cmd+ctrl" }]].

EventSensor class >> mapControlKeysToCommandKeys
mapControlKeysToCommandKeys
	<preference: 'Map CONTROL keys to COMMAND keys'
		categoryList: #(keyboard events input)
		description: 'On platforms other than macOS, users use the CONTROL modifier for many common shortcuts around text editing such as CTRL+C and CTRL+V for copy and paste. For the sake of cross-platform compatibility, however, many tools in this system will check for CMD+C and CMD+V instead. Note that on Linux and Windows platforms, keystrokes involving the (physical) ALT modifier typically arrive as COMMAND modifier. Thus, enable this preference if you still want to use CONTROL key instead of the ALT key for such shortcuts. (This preference replaces older duplicate/swap preferences.)'
		type: #Boolean>
		
	^ KeyDecodePreferences
		at: #mapControlKeysToCommandKeys
		ifAbsent: [true]

EventSensor class >> mapControlKeysToCommandKeys:
mapControlKeysToCommandKeys: aBooleanOrNil

	aBooleanOrNil
		ifNil: [
			KeyDecodePreferences
				removeKey: #mapControlKeysToCommandKeys
				ifAbsent: []]
		ifNotNil: [
			KeyDecodePreferences
				at: #mapControlKeysToCommandKeys
				put: aBooleanOrNil].

	self installKeyDecodeTable.
	self installVirtualKeyTable.

Preferences class >> preserveCommandExcursions
preserveCommandExcursions
	"An architecture is in place for storing command excursions to which access is otherwise cut off by having taken a variant branch, but it is not accessible unless you hand-code this preference to true -- which I suggest you do only with fingers crossed."

	^ false

SmalltalkImage >> commandLine
commandLine
	"Answer the object to query about command line."
	
	^self

SmalltalkImage >> handleUserInterrupt
handleUserInterrupt
	[ToolSet handleUserInterruptRequest: 'User Interrupt'] fork.

TextEditor class >> initializeCmdKeyShortcuts
initializeCmdKeyShortcuts
	"Initialize the (unshifted) command-key (or alt-key) shortcut table."

	"NOTE: if you don't know what your keyboard generates, use Sensor kbdTest"

	"TextEditor initialize"

	| cmdMap cmds |
	cmdMap := Array new: 256 withAll: #noop:.		"use temp in case of a crash"
	cmdMap at: 1 + 1 put: #cursorHome:.				"home key"
	cmdMap at: 4 + 1 put: #cursorEnd:.				"end key"
	cmdMap at: 8 + 1 put: #backspace:.				"ctrl-H or delete key"
	cmdMap at: 11 + 1 put: #cursorPageUp:.			"page up key"
	cmdMap at: 12 + 1 put: #cursorPageDown:.		"page down key"
	cmdMap at: 13 + 1 put: #crWithIndent:.			"cmd-Return"
	cmdMap at: 27 + 1 put: #offerMenuFromEsc:.		"escape key"
	cmdMap at: 28 + 1 put: #cursorLeft:.				"left arrow key"
	cmdMap at: 29 + 1 put: #cursorRight:.				"right arrow key"
	cmdMap at: 30 + 1 put: #cursorUp:.				"up arrow key"
	cmdMap at: 31 + 1 put: #cursorDown:.				"down arrow key"
	cmdMap at: 32 + 1 put: #selectWord:.				"space bar key"
	cmdMap at: 127 + 1 put: #forwardDelete:.		"del key"
			
	'0123456789' 
		do: [:char | cmdMap at: char asciiValue + 1 put: #changeEmphasis:].
	
	cmds := #($a #selectAll: $c #copySelection: $e #exchange: $f #find: $g #findAgain: $j #doAgain: $k #offerFontMenu: $u #align: $v #paste: $w #backWord: $x #cut: $y #swapChars: $z #undo:).
	1 to: cmds size
		by: 2
		do: [:i | cmdMap at: (cmds at: i) asciiValue + 1 put: (cmds at: i + 1)].
		
	cmdActions := cmdMap

SmalltalkEditor class >> initializeCmdKeyShortcuts
initializeCmdKeyShortcuts
	"Initialize the (unshifted) command-key (or alt-key) shortcut table."
	"NOTE: if you don't know what your keyboard generates, use Sensor kbdTest"
	"SmalltalkEditor initialize"
	| cmds |
	super initializeCmdKeyShortcuts.
	cmds := #($b #browseIt: $d #doIt: $i #inspectIt: $l #cancel: $m #implementorsOfIt: $n #sendersOfIt: $o #spawnIt: $p #printIt: $q #querySymbol: $s #save: ).
	1 to: cmds size
		by: 2
		do: [ : i | cmdActions at: (cmds at: i) asciiValue + 1 put: (cmds at: i + 1)].
	"Set up type-method argument hot keys, 1-4.."
	'1234' do:
		[ : eachKeyboardChar |
		cmdActions 
			at: eachKeyboardChar asciiValue + 1
			put: #typeMethodArgument: ]

TextEditor class >> initializeShiftCmdKeyShortcuts
initializeShiftCmdKeyShortcuts 
	"Initialize the shift-command-key (or control-key) shortcut table."
	"NOTE: if you don't know what your keyboard generates, use Sensor kbdTest"
	"wod 11/3/1998: Fix setting of cmdMap for shifted keys to actually use the 
	capitalized versions of the letters.
	TPR 2/18/99: add the plain ascii values back in for those VMs that don't return the shifted values."

	"TextEditor initialize"
	
	| cmdMap cmds |

	"shift-command and control shortcuts"
	cmdMap := Array new: 256 withAll: #noop:.  		"use temp in case of a crash"
	cmdMap at: ( 1 + 1) put: #cursorHome:.			"home key"
	cmdMap at: ( 4 + 1) put: #cursorEnd:.				"end key"
	cmdMap at: ( 8 + 1) put: #forwardDelete:.			"ctrl-H or delete key"
	cmdMap at: (11 + 1) put: #cursorPageUp:.			"page up key"
	cmdMap at: (12 + 1) put: #cursorPageDown:.		"page down key"
	cmdMap at: (13 + 1) put: #crWithIndent:.			"ctrl-Return"
	cmdMap at: (27 + 1) put: #offerMenuFromEsc:.	"escape key"
	cmdMap at: (28 + 1) put: #cursorLeft:.			"left arrow key"
	cmdMap at: (29 + 1) put: #cursorRight:.			"right arrow key"
	cmdMap at: (30 + 1) put: #cursorUp:.				"up arrow key"
	cmdMap at: (31 + 1) put: #cursorDown:.			"down arrow key"
	cmdMap at: (32 + 1) put: #selectWord:.			"space bar key"
	cmdMap at: (45 + 1) put: #changeEmphasis:.		"cmd-sh-minus"
	cmdMap at: (61 + 1) put: #changeEmphasis:.		"cmd-sh-plus"
	cmdMap at: (127 + 1) put: #forwardDelete:.		"del key"

	cmds := #(
		$c	compareToClipboard:
		$h	cursorTopHome:
		$j	doAgainUpToEnd:
		$k	changeStyle:
		$m	selectCurrentTypeIn:
		$s	findAgain:
		$u	changeLfToCr:
		$x	makeLowercase:
		$y	makeUppercase:
		$z	redo: "makeCapitalized:"
	).
	1 to: cmds size by: 2 do: [ :i |
		cmdMap at: ((cmds at: i) asciiValue + 1) put: (cmds at: i + 1).			"plain keys"
		cmdMap at: ((cmds at: i) asciiValue - 32 + 1) put: (cmds at: i + 1).		"shifted keys"
		cmdMap at: ((cmds at: i) asciiValue - 96 + 1) put: (cmds at: i + 1).		"ctrl keys"
	].
	shiftCmdActions := cmdMap

SmalltalkEditor class >> initializeShiftCmdKeyShortcuts
initializeShiftCmdKeyShortcuts 
	"Initialize the shift-command-key (or control-key) shortcut table."
	"NOTE: if you don't know what your keyboard generates, use Sensor kbdTest"
	| cmds |
	super initializeShiftCmdKeyShortcuts.
	cmds := #(
		$a	argAdvance:
		$b	browseItHere:
		$d	debugIt:
		$e	methodStringsContainingIt:
		$f	displayIfFalse:
		$g	fileItIn:
		$i	exploreIt:
		$n	referencesToIt:
		$s	invokePrettyPrint:
		$t	displayIfTrue:
		$v	pasteInitials:
		$w	methodNamesContainingIt:
	).
	1 to: cmds size by: 2 do: [ :i |
		shiftCmdActions at: ((cmds at: i) asciiValue + 1) put: (cmds at: i + 1).			"plain keys"
		shiftCmdActions at: ((cmds at: i) asciiValue - 32 + 1) put: (cmds at: i + 1).		"shifted keys"
		shiftCmdActions at: ((cmds at: i) asciiValue - 96 + 1) put: (cmds at: i + 1).		"ctrl keys"
	].
	"shift+cmd _ (underscore)"
	shiftCmdActions at: $_ asciiValue+1 put: #flattenSelection:

