[squeak-dev] Please try out | Cross-platform mapping for virtual key codes :-)

David T. Lewis lewis at mail.msen.com
Sat May 15 15:17:08 UTC 2021


Hi Marcel,

Attached are changes that should make the virtual key mapping work
when the image is restarted on a different platform.

Dave


On Wed, Apr 28, 2021 at 12:02:44PM +0200, Marcel Taeumel wrote:
> Hi all!
> 
> Here is a small update. Please find attached the changeset.
> 
> Updates:
> - Adds KeyboardEvent >> #keyCode (via new inst-var)
> - Logs the last key-down event to attach virtual-key codes to key-stroke events; see HandMorph >> #generateKeyboardEvent:
> - Simplifies KeyboardEvent >> #key
> - Show event repetition in KeyboardExecizer
> 
> 
> 
> Major questions:
> 1. Does it work on your machine?
> 2. What are your thoughts on KeyboardEvent >> #key?
> 3. What are your thoughts on KeyboardEvent >> #keyCode?
> 4. Do you understand KeyboardEvent >> #physicalKey #virtualKey #physicalModifiers #virtualModifiers ?
> 
> Happy testing!
> 
> Best,
> Marcel
> 
> P.S.: Don't forget about the X11 key (scan?) codes. ^__^ I haven't had the time to look into the VM plugin yet.
> Am 27.04.2021 16:40:56 schrieb Marcel Taeumel <marcel.taeumel at hpi.de>:
> Hi all!
> 
> 
> Please find attached a changeset that adds mapping tables for virtual keys (or scan codes) for macOS, X11, and Windows. You can find them in EventSensor class >> #virtualKeysOn*
> 
> You can try out if they work through the KeyboardExerciser. Please take a look at the balloon text (i.e. tool tip) to better understand the data.
> 
> There is also a new preference:
> [x] Simplify Virtual-key codes
> 
> ... because of Windows who dares to couple codes to the input language (e.g. US vs. DE), which Squeak knows nothing about. macOS is better in this regard. :-)
> 
> Biggest mess is on Linux/X11. For key-down/up events, the Linux VM delivers actual character codes instead of scan codes, which makes a basic mapping to physical keys almost impossible. See EventSensor class >> #virtualKeysOnX11. We MUST fix that! Please. Somebody. Can I haz scan codes? ^__^
> 
> ***
> 
> 
> ***
> 
> The good news: KeyboardEvent >> #key (and UserInputEvent >> #modifiers) now gives you cross-platform stable information about physical keys to be used in keyboard handlers. Yes, for both key-stroke and key-down/up events.
> 
> Or at least, that is the plan. That's why it would be great if you could help testing! :-)
> 
> Why key-stroke events too? Aren't they for typing text only?
> 
> 1. Almost all keyboard shortcuts in current Squeak are based on key-stroke events.
> 2. Using the #keyCharacter is tricky because SHIFT changes lower-case to upper-case, which makes "anEvent shiftPressed" hard to understand.
> 3. CTRL combinations might not do the expected thing. How would you handle CTRL+C? The #keyCharacter could arrive as $c or Character enter. See the preference "Map non-printable characters to printable characters. Now, #key will always answer $C in such a case. Regardless of that preference.
> 
> Can't we just use #keyCharacter in key-down/up events?
> 
> No. Those are undefined. Never do that. key-down/up events carry virtual-key codes in their #keyValue. We might want to change #keyCharacter to answer "nil" for those events.
> 
> ***
> 
> Q: What is a "physical key" or "physical modifier"?
> A: The label that can be presented to the user so that he or she feels at home when using Squeak. Thus, looks platform-specific.
> 
> Q: What is a "virtual key" or "virtual modifier"?
> A: The information to be processed in your application's key handlers. Thus, looks platform-independent. If you have still no clue how to talk to keyboard events, please read the commentary in KeyboardEvent >> #checkCommandKey.
> 
> ***
> 
> Happy testing! :-) And thank you all in advance!
> 
> Best,
> Marcel
> 
> P.S.: You might want to disable the preference "synthesize mouse-wheel events from keyboard-events" to get CTRL+ArrowUp and CTRL+ArrowDown ;-)




> 

-------------- next part --------------
'From Squeak6.0alpha of 12 May 2021 [latest update: #20523] on 14 May 2021 at 11:24:34 pm'!
"Change Set:		EventSensor-dtl
Date:			14 May 2021
Author:			David T. Lewis

Some tweaks to Marcel's key mapping. Simplify a case statement and arrange for initialization at image startup on a possibly different platform"!


!EventSensor class methodsFor: 'class initialization' stamp: 'dtl 5/14/2021 23:01'!
installVirtualKeyTable

	VirtualKeyTable := Dictionary newFrom: (
		Smalltalk windowSystemName
			caseOf: {
				['Windows'] -> [self virtualKeysOnWindows].
				['Win32' "older VMs"] -> [self virtualKeysOnWindows].
				['Aqua'] -> [self virtualKeysOnMacOS].
				['X11'] -> [self virtualKeysOnX11].
				['RiscOS'] -> [{}].
				['Quartz'] -> [{}].
			} otherwise: [{}]).
	
	"Shift 8 bits to not overwrite virtual-key mappings from above."
	self mapControlKeysToCommandKeys ifTrue: [		
		VirtualKeyTable
			at: (2r0010 "ctrl" bitShift: 8)
			put: (2r1010 "cmd+ctrl").
		VirtualKeyTable
			at: (2r0011 "ctrl+shift" bitShift: 8)
			put: (2r1011 "cmd+ctrl+shift")].
		
	self mapAltKeysToOptionKeys ifTrue: [
		VirtualKeyTable
			at: (2r1000 "cmd/alt" bitShift: 8)
			put: (2r1100 "cmd/alt+opt").
		VirtualKeyTable
			at: (2r1001 "cmd/alt+shift" bitShift: 8)
			put: (2r1101 "cmd/alt+opt+shift")].! !

!EventSensor class methodsFor: 'system startup' stamp: 'dtl 5/14/2021 23:07'!
startUp: resuming
	
	resuming ifTrue: [
		Smalltalk platformName = 'Mac OS'
			ifTrue: [
				self mapAltKeysToOptionKeys: false.
				self mapControlKeysToCommandKeys: false]
			ifFalse: [
				self mapAltKeysToOptionKeys: true.
				self mapControlKeysToCommandKeys: true].
		self installVirtualKeyTable.
		self default startUp ].! !

EventSensor class removeSelector: #startUp!


More information about the Squeak-dev mailing list