Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Tk : Translating Keysyms to eqiv Tk modifier strings.

9 views
Skip to first unread message

Jinesh Choksi

unread,
Jan 10, 2000, 3:00:00 AM1/10/00
to
Feature Request for Tk

Recently I wanted to provide the user with the ability to easily specify
the
key bindings for a specific task. E.g. The user places the input focus
onto an
entry widget and presses the keys Control-a. This would mean that they
wanted a binding such as <Control-Key-a> on something. But, the problem
is, to detect
which keys the user has pressed in the entry widget, I need to have some
code
like the following :

bind $entrywidget <KeyPress> {{puts stdout {{Keysym = %K Char =
%A}}

So...the %K tells me what keysyms were involved but how do I translate
those
keysyms into appropriate modifiers ? E.g. If the Keysysm was a Control_L
then
the modifier should be Control. Easy enough ! But what if the keysym was
something like Super_R(windows key) ?? Of course the modifier should be
one of
the following (M1,M2,M3,M4,M5) depending on how the system is setup !
There's no way in Tk to figure out what keysym's the modifiers are
generating and hence translating keysyms into modifier strings involves
using hacks like checking the output from "xmodmap" or ripping out the
source in tkBind.c which provides a function that returns the modifier
string for a particular keysym.

Neither method is very appealing in terms of portability and elegance.
It would be extremely usefull if the bind command was extended, so as to
provide a option which returns the modifier string for a particular
keysym.

Jinesh Choksi

jay

unread,
Jan 11, 2000, 3:00:00 AM1/11/00
to
In article <3879E574...@cassini.f9.co.uk>,

jch...@bigfoot.com wrote:
> bind $entrywidget <KeyPress> {{puts stdout {{Keysym = %K Char =
> %A}}
>
> So...the %K tells me what keysyms were involved but how do I translate
> those keysyms into appropriate modifiers ?

You need to look at the state, the %s value. Here's a simple example:

proc keyBinding { entry sym state } {
global keybinding
# These come from /usr/include/X11/X.h but
# seem to be valid on windows systems as well.
set ShiftMask [expr 1<<0]
set LockMask [expr 1<<1]
set ControlMask [expr 1<<2]
set Mod1Mask [expr 1<<3]
set Mod2Mask [expr 1<<4]
set Mod3Mask [expr 1<<5]
set Mod4Mask [expr 1<<6]
set Mod5Mask [expr 1<<7]
set keybinding {}
if {[expr $state & $ControlMask]} { append keybinding {Control-} }
if {[expr $state & $LockMask]} { append keybinding {Lock-} }
if {[expr $state & $Mod1Mask]} { append keybinding {Mod1-} }
if {[expr $state & $Mod2Mask]} { append keybinding {Mod2-} }
if {[expr $state & $Mod3Mask]} { append keybinding {Mod3-} }
if {[expr $state & $Mod4Mask]} { append keybinding {Mod4-} }
if {[expr $state & $Mod5Mask]} { append keybinding {Mod5-} }
if {[expr $state & $ShiftMask]} { append keybinding {Shift-} }
append keybinding $sym
$entry delete 0 end
$entry insert 0 $keybinding
}

entry .e -width 40
pack .e
bind .e <Key> { keyBinding %W %K %s }

One problem with this example is that you'll get the key sym of the
modifier key displayed as well. If you were willing to restrict to only
alpha-numeric keys, you could weed those mostly out by using the %A
value. But if you want to allow something like Control-Delete, you have
to use keysyms. And of course, there are other problem keys as well,
such as Enter -- do you use the %A value (\r) or the keysym name?

Which is why this is just a simple example. HTH!

: jay
--
Using self-discipline, see http://www.eiffel.com/discipline


Sent via Deja.com http://www.deja.com/
Before you buy.

0 new messages