Scala Swing Mnemonics

89 views
Skip to first unread message

goddard

unread,
Mar 19, 2011, 8:38:15 AM3/19/11
to scala-user
Hi,

how can I set a mask, for example if I've got:

object AddTweetButton extends Button("+".tagB.tagHtml) {
mnemonic = Key.N
tooltip = Loc("newWindow")
}

and I'd like to press Ctrl + N to get a new window. The default is
Alt.

Regards, Jiri

Ken Scambler

unread,
Mar 19, 2011, 8:24:27 PM3/19/11
to goddard, scala-user
It simply delegates to the Java functionality.

The javadoc says:

"The mnemonic is the key which when combined with the look and feel's mouseless modifier (usually Alt) will activate this button if focus is contained somewhere within this button's ancestor window.  A mnemonic must correspond to a single key on the keyboard...".

So the mnemonic key seems to be a property of the L&F. 

It might be better to roll your own by listening to keys pressed.

Ken

Goddard Jiri

unread,
Mar 20, 2011, 8:41:50 AM3/20/11
to Ken Scambler, scala-user
Thanks Ken,

the thing is that I use platform L&F, so on windows it should be Ctrl + N. Seems like I'll have to make my own.
By the way, do you know how to use the Key.Modifier properly? I don't quite understand the reason it's there.
It's probably because the Scaladoc is so loosely connected to Javadoc.

Regards, Jiri
--
web: http://www.dredwerkz.cz
group: http://groups.google.com/group/dr3dwerkz
music: http://profile.ultimate-guitar.com/g0dd4rd/
twitter: http://twitter.com/#!/g0dd4rd
profile: http://www.google.com/profiles/g0dd4rd
icq: 218 659 431

Ken Scambler

unread,
Mar 20, 2011, 7:42:04 PM3/20/11
to Goddard Jiri, scala-user
Hi Jiri,


By the way, do you know how to use the Key.Modifier properly? I don't quite understand the reason it's there.
 
Again, this functionality delegates straight to Java, namely the bit-masks in java.awt.event.InputEvent.  I think the reason for the Key.Modifier module is to expose these constants in an idiomatic Scala style, ie. not all capitals and underscores.

You use the 'modifiers' integer by using the bit-wise operators:

val ev: java.awt.event.InputEvent = ...
int mod = ev.getModifiers()
val hasAlt = (mod & Key.Modifier.Alt) != 0
val hasControl = (mod & Key.Modifier.Control) != 0

Bitfields always remind me of C++ :)

Cheers,
Ken

Ken Scambler

unread,
Mar 20, 2011, 7:48:49 PM3/20/11
to Goddard Jiri, scala-user
I should mention the Scala version of listening to an event would of course be:

fooComponent.reactions += {
  case KeyPressed(_, _, mods, _) =>
    val hasAlt = (mod & Key.Modifier.Alt) != 0
    // etc
}

Ken

Goddard Jiri

unread,
Mar 21, 2011, 4:02:33 AM3/21/11
to Ken Scambler, scala-user
Thanks again :)
Apparently I should not code if I can't figure out a simple &.

Regards, Jiri
Reply all
Reply to author
Forward
0 new messages