Still keyboarding morphs

Ned Konz ned at bike-nomad.com
Sat Dec 22 18:26:29 UTC 2001


On Friday 21 December 2001 07:14 pm, Jerry Balzano wrote:


> Awhile back, Ned Konz passed me some code (submitted to this list as
> "KeyboardListenerDemo-nk.1.cs" on 10/26/01) to make a morph respond to
> KB input by e.g. moving around the screen.  Here it is in a nutshell
> (skip down to "...." if you don't care about the details of the code):

[snip]

> on: keyChar do: aBlock


Hmm... My copy (KeyboardListenerDemo-nk.3.cs, enclosed) has on:do: removed, 
and uses onKeystroke:do:.

> (1) First of all (skipping lots of steps), are the arrow keys
> represented by the numbers 28-31 or the numbers 123-126?  The code
> above actually responds both to the arrow keys and to other keys (e.g.
> the letter "o" works like the uparrow and the char "]" works like the
> downarrow).

Mine doesn't respond to the other keys, just the arrow keys.

Character arrowDown is 31.

> (2) A modified version of the above code (submitted to this list as
> "KeyboardListenerDemo-nk.2.cs" on 10/26/01) doesn't respond to the
> arrow keys at all but only other keys (that I eventually found) on the
> KB like "o" and "]".

It's broken, then. Try this one.

> I really like the idea of incorporating KB control of morphs into the
> scripting system, but isn't there a way to do it that would allow a
> more localized, concurrent way of creating the scripts?

The problem is that there is a single, central queue that gets the events, 
and they have to be processed. The Playfield wants to do keyboard steering 
for mouse focus. Certainly the keyboard listener hook that I used is the 
easiest way to get into this queue.

Another architectural approach would be to have a single listener that 
notifies interested objects of keystroke events. It's easy to try this as 
well.

There is still the problem of the keyboard steering vs. global keyboard 
listening (if a text morph has the focus should anyone else hear about the 
keystrokes?).

-- 
Ned Konz
currently: Stanwood, WA
email:     ned at bike-nomad.com
homepage:  http://bike-nomad.com

--------------Boundary-00=_5KCRF1N3ZCITDD014IV6
Content-Type: text/plain;
  charset="iso-8859-1";
  name="KeyboardListenerDemo-nk.3.cs"
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="KeyboardListenerDemo-nk.3.cs"

'From Squeak3.2alpha of 3 October 2001 [latest update: #4441] on 26 October 2001 at 6:21:19 pm'!
"Change Set:		KeyboardListenerDemo-nk
Date:			26 October 2001
Author:			Ned Konz

KeyboardTestMorph demo

This shows how you can use addKeyboardListener: to hook keyboard events."!

Morph subclass: #KeyboardTestMorph
	instanceVariableNames: 'keyMapping delta '
	classVariableNames: ''
	poolDictionaries: ''
	category: 'NK-Demo'!

!KeyboardTestMorph commentStamp: 'nk 10/26/2001 18:08' prior: 0!
This is a little demo of using the HandMorph>>addKeyboardListener functions.
Just go:
KeyboardTestMorph demo!


!KeyboardTestMorph methodsFor: 'events-processing' stamp: 'nk 10/26/2001 17:48'!
handleListenEvent: evt
	| mapping |
	mapping _ keyMapping at: evt keyValue ifAbsent: [ ^self ].
	(mapping at: evt type ifAbsent: [ ^self ]) value: self! !

!KeyboardTestMorph methodsFor: 'events-processing' stamp: 'nk 10/26/2001 17:46'!
onKeyDown: keyCharacter do: aValuableObject
	(keyMapping at: keyCharacter asInteger ifAbsentPut: [ IdentityDictionary new ])
		at: #keyDown
		put: aValuableObject.! !

!KeyboardTestMorph methodsFor: 'events-processing' stamp: 'nk 10/26/2001 17:46'!
onKeyUp: keyCharacter do: aValuableObject
	(keyMapping at: keyCharacter asInteger ifAbsentPut: [ IdentityDictionary new ])
		at: #keyUp
		put: aValuableObject.! !

!KeyboardTestMorph methodsFor: 'events-processing' stamp: 'nk 10/26/2001 17:47'!
onKeystroke: keyCharacter do: aValuableObject
	(keyMapping at: keyCharacter asInteger ifAbsentPut: [ IdentityDictionary new ])
		at: #keystroke
		put: aValuableObject.! !

!KeyboardTestMorph methodsFor: 'initialization' stamp: 'nk 10/26/2001 17:59'!
initialize
	super initialize.
	delta _ 0 at 0.
	keyMapping _ IdentityDictionary new.
	self currentHand addKeyboardListener: self. ! !

!KeyboardTestMorph methodsFor: 'submorphs-add/remove' stamp: 'nk 10/26/2001 17:51'!
delete
	self currentHand removeKeyboardListener: self.
	^super delete.! !

!KeyboardTestMorph methodsFor: 'stepping and presenter' stamp: 'nk 10/26/2001 17:54'!
step
	delta isZero ifFalse: [
		self position: self position + delta
	]! !

!KeyboardTestMorph methodsFor: 'stepping and presenter' stamp: 'nk 10/26/2001 18:07'!
stepTime
	^20! !

!KeyboardTestMorph methodsFor: 'accessing' stamp: 'nk 10/26/2001 18:02'!
delta: aPoint
	delta _ aPoint.
! !

!KeyboardTestMorph methodsFor: 'accessing' stamp: 'nk 10/26/2001 17:57'!
onKey: aKeycode moveBy: aDelta
	self onKeyDown: aKeycode do: [ :k | self delta: aDelta ].
	self onKeyUp: aKeycode do: [ :k | self delta: 0 at 0 ].! !


!KeyboardTestMorph class methodsFor: 'as yet unclassified' stamp: 'nk 10/26/2001 18:06'!
demo
	"KeyboardTestMorph demo"
	"Opens up two KeyboardTestMorphs that you can control with the arrow keys"

| m1 m2 increment |

increment _ 0 at 10.

m1 _ KeyboardTestMorph new.
m1 onKey: Character arrowDown moveBy: increment.
m1 onKey: Character arrowUp moveBy: increment negated.
m1 position: 20 at 200; openInWorld.

m2 _ KeyboardTestMorph new.
m2 onKey: Character arrowRight moveBy: increment.
m2 onKey: Character arrowLeft moveBy: increment negated.
m2 position: 200 at 200; color: Color red; openInWorld.! !

KeyboardTestMorph removeSelector: #fred!
KeyboardTestMorph removeSelector: #on:do:!


More information about the Squeak-dev mailing list