Version 0 of Portable keystate

Updated 2002-10-05 21:25:15

ulis, 2002-10-05.

It is not so easy to deal with keystate (shift, control,...) in a portable manner.

Here is a portable method:

Each key press or key release of the control and shift keys is trapped, and its state saved in a global. ---

  set ::Control 0
  set ::Shift 0
  event add <<ControlOn>>   <KeyPress-Control_L>    <KeyPress-Control_R>
  event add <<ControlOff>>  <KeyRelease-Control_L>  <KeyRelease-Control_R>
  event add <<ShiftOn>>     <KeyPress-Shift_L>      <KeyPress-Shift_R>
  event add <<ShiftOff>>    <KeyRelease-Shift_L>    <KeyRelease-Shift_R>
  bind . <<ControlOn>>  \
  { 
    set ::Control 1
    .s.c config -text "control on" -fg red
    break 
  }
  bind . <<ControlOff>> \
  { 
    set ::Control 0
    .s.c config -text "control off" -fg black
    break 
  }
  bind . <<ShiftOn>>    \
  { 
    set ::Shift 1
    .s.s config -text "shift on" -fg red
    break 
  }
  bind . <<ShiftOff>>   \
  { 
    set ::Shift 0
    .s.s config -text "shift off" -fg black
    break 
  }
  bind . <KeyPress> \
  { 
    set key %K
    if {$::Shift}   { set key Shift-$key } 
    if {$::Control} { set key Ctrl-$key } 
    .k.k config -text $key
  }
  frame .s
  label .s.c -width 15 -text "control off" -bd 2 -relief ridge
  label .s.s -width 15 -text "shift off"   -bd 2 -relief ridge
  frame .k
  label .k.l -width 15 -text "last key"
  label .k.k -width 15 -bd 1 -relief sunken -bg white
  pack .s.c .s.s .k.l .k.k -padx 20 -pady 5
  pack .s .k -pady 10
  focus -force .
  raise .

---