Version 5 of Entering Unicode characters in a widget

Updated 2008-01-23 15:14:08 by MJ

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

 text .t
 entry .e
 pack .t
 pack .e

 # enable functionality by adding the UnicodeEntry tag to the bindtags
 bindtags .t [list .t UnicodeEntry Text . all]
 bindtags .e [list .e UnicodeEntry Entry . all]


 namespace eval unicode_entry {
   variable uc_keys

   proc enable_unicode_entry {widget} {
      variable uc_keys
      set uc_keys($widget) {}
   }

   proc disable_unicode_entry {widget} {
      variable uc_keys
      unset -nocomplain uc_keys($widget)
   }

   proc handle_uc_key {widget key} {
      variable uc_keys
      if {![info exists uc_keys($widget)]} {
         return
      }

      upvar 0 uc_keys($widget) keys
      switch -glob -- [string toupper $key] {
        {[0-9A-F]} {
            append keys $key
            if {[string length $keys] >= 4} {
               $widget insert insert [subst \\u$keys]
               disable_unicode_entry $widget
            }
            return -code break
        }
        default {
            $widget insert insert $keys
            disable_unicode_entry $widget
          }      
        }                
   }

   bind UnicodeEntry <Control-Key-u> [namespace code [list enable_unicode_entry %W]]
   bind UnicodeEntry <Key> [namespace code [list handle_uc_key %W %A]]
 }

See also: KHIM