Version 1 of Entering Unicode characters in a widget

Updated 2008-01-23 14:01:36 by MJ

MJ - The following script adds the <Control-.> binding to text or entry widgets which allows entry of Unicode characters directly.

 package require Tcl 8.5
 ext .t
 entry .e
 pack .t
 pack .e

 variable uc_keys

 proc enable_unicode_entry {widget} {
      variable uc_keys
      set uc_keys($widget) {}
      bindtags $widget [list UnicodeEntry {*}[bindtags $widget]] 
 }

 proc handle_uc_key {widget key} {
  variable uc_keys
  upvar 0 uc_keys($widget) keys
  switch -nocase -regexp -- $key {
        {[0-9A-F]} {
           append keys $key
           if {[string length $keys] == 4} {
                $widget insert insert [subst \\u$keys]
                bindtags $widget [lrange [bindtags $widget] 1 end]  
           }
           return -code break
        }
        default {
                $widget insert insert $keys
                bindtags $widget [lrange [bindtags $widget] 1 end]  
        }      
  }                
 }


 bind UnicodeEntry <Key> {handle_uc_key %W %A}


 bind .t <Control-.> {
        enable_unicode_entry %W
 }

 bind .e <Control-.> {
        enable_unicode_entry %W
 }

See also: KHIM