def getchar
      $key_chr = nil
        c = nil
        while true
          c = self.getch
          break if c != -1
        end
    
        cn = c
        $key_int = c
        # handle control codes 0 to 127 but not escape
        if cn >= 0 && cn < 128 && cn != 27
          #$key_chr = key_tos(c)
          return c
        end
        
        # if escape then get into a loop and keep checking till -1 or another escape
        #
        if c == 27
          buff=c.chr
          # if there is another escape coming through then 2 keys were pressed so
          # evaluate upon hitting an escape
          # NOTE : i think only if ESc is followed by [ should be keep collectig
          # otherwise the next char should evaluate. cases like F1 are already being sent in as high integer codes
          while true
            #$log.debug " #{Time.now.to_f} inside LOOP before getch "
            # This getch seems to take enough time not to return a -1 for almost a second
            # even if nodelay is true ??? XXX
            FFI::NCurses.set_escdelay(5)
            k = self.getch
            #$log.debug "elapsed #{elapsed} millis  inside LOOP AFTER getch #{k} (#{elapsed1})"
            $log.debug "inside LOOP AFTER getch #{k} "

            if k == 27
              # seems like two Meta keys pressed in quick succession without chance for -1 to kick in
              # but this still does not catch meta char followed by single char. M-za , it does.
              if $esc_esc
                if buff == 27.chr
                  $key_chr = "<ESC-ESC>"
                  return 2727
                else
                  alert "buff is #{buff}"
                end
              end
              $log.debug "  1251 before evaluate "
              x = _evaluate_buff buff
              # return ESC so it can be interpreted again.
              @window.ungetch k
              $key_chr = x if x
              return $key_int if x
              $log.warn "getchar: window.rb 1200 Found no mapping for #{buff} "
              $key_chr = buff
              return $key_int
              #return buff # otherwise caught in loop ???
            elsif k > -1
              # FIXME next lne crashes if M-C-h pressed which gives 263
              if k > 255
                $log.warn "getchar: window.rb 1247 Found no mapping for #{buff} #{k} "
                $key_int = k + 128
                return $key_int
                # this contains ESc followed by a high number
=begin
                ka = key_tos(k)
                if ka
                  $key_chr = "<M-" + ka[1..-1]
                  $key_int = k + 128
                  return $key_int
                else
                  $key_chr = "UNKNOWN: Meta + #{k}"
                  return 9999
                end
=end
              end

              buff += k.chr
              # this is an alt/meta code. All other complex codes seem to have a [ after the escape
              # so we will keep accumulating them.
              # NOTE this still means that user can press Alt-[ and some letter in quick succession
              # and it will accumulate rather than be interpreted as M-[.
              #
              if buff.length == 2 and k == 79
                # this is Alt-O and can be a F key in some terms like xterm-color
              elsif buff.length == 2 and k.chr != '['
                x = _evaluate_buff buff
        
                $key_chr = x
                return $key_int if x
              end
              #$log.debug "XXX:  getchar adding #{k}, #{k.chr} to buff #{buff} "
            else
              #$log.debug "  GOT -1 in escape "
              # it is -1 so evaluate
              x = _evaluate_buff buff
              $key_chr = x if x
              return $key_int if x
              $log.warn "getchar: window.rb 1256 Found no mapping for #{buff} "
              $key_chr = buff
              return $key_int
            end
          end
        end
        
        # what if keyname does not return anything
        if c > 127
          #$log.info "xxxgetchar: window.rb sending #{c} "
=begin
          ch =  FFI::NCurses::keyname(c) 
          # remove those ugly brackets around function keys
          if ch && ch[-1]==')'
            ch = ch.gsub(/[()]/,'')
          end
          if ch && ch.index("KEY_")
            ch = ch.gsub(/KEY_/,'')
          end
          ch = "<#{ch}>" if ch
          #return ch if ch
          $key_chr = ch if ch
          $key_chr = "UNKNOWN:#{c}" unless ch
          $log.warn "getchar: window.rb 1234 Found no mapping for #{c} " unless ch
=end
          #$key_chr = key_tos(ch)
          return c
        end
        if c
          #$key_chr =  c.chr 
          return c 
        end
    end
def getchar
      $key_chr = nil
        c = nil
        while true
          c = self.getch
          break if c != -1
        end
    
        cn = c
        $key_int = c
        # handle control codes 0 to 127 but not escape
        if cn >= 0 && cn < 128 && cn != 27
          #$key_chr = key_tos(c)
          return c
        end
        
        # if escape then get into a loop and keep checking till -1 or another escape
        #
        if c == 27
          buff=c.chr
          # if there is another escape coming through then 2 keys were pressed so
          # evaluate upon hitting an escape
          # NOTE : i think only if ESc is followed by [ should be keep collectig
          # otherwise the next char should evaluate. cases like F1 are already being sent in as high integer codes
          while true
            #$log.debug " #{Time.now.to_f} inside LOOP before getch "
            # This getch seems to take enough time not to return a -1 for almost a second
            # even if nodelay is true ??? XXX
            FFI::NCurses.set_escdelay(5)
            k = self.getch
            #$log.debug "elapsed #{elapsed} millis  inside LOOP AFTER getch #{k} (#{elapsed1})"
            $log.debug "inside LOOP AFTER getch #{k} "

            if k == 27
              # seems like two Meta keys pressed in quick succession without chance for -1 to kick in
              # but this still does not catch meta char followed by single char. M-za , it does.
              if $esc_esc
                if buff == 27.chr
                  $key_chr = "<ESC-ESC>"
                  return 2727
                else
                  alert "buff is #{buff}"
                end
              end
              $log.debug "  1251 before evaluate "
              x = _evaluate_buff buff
              # return ESC so it can be interpreted again.
              @window.ungetch k
              $key_chr = x if x
              return $key_int if x
              $log.warn "getchar: window.rb 1200 Found no mapping for #{buff} "
              $key_chr = buff
              return $key_int
              #return buff # otherwise caught in loop ???
            elsif k > -1
              # FIXME next lne crashes if M-C-h pressed which gives 263
              if k > 255
                $log.warn "getchar: window.rb 1247 Found no mapping for #{buff} #{k} "
                $key_int = k + 128
                return $key_int
                # this contains ESc followed by a high number
=begin
                ka = key_tos(k)
                if ka
                  $key_chr = "<M-" + ka[1..-1]
                  $key_int = k + 128
                  return $key_int
                else
                  $key_chr = "UNKNOWN: Meta + #{k}"
                  return 9999
                end
=end
              end

              buff += k.chr
              # this is an alt/meta code. All other complex codes seem to have a [ after the escape
              # so we will keep accumulating them.
              # NOTE this still means that user can press Alt-[ and some letter in quick succession
              # and it will accumulate rather than be interpreted as M-[.
              #
              if buff.length == 2 and k == 79
                # this is Alt-O and can be a F key in some terms like xterm-color
              elsif buff.length == 2 and k.chr != '['
                x = _evaluate_buff buff
        
                $key_chr = x
                return $key_int if x
              end
              #$log.debug "XXX:  getchar adding #{k}, #{k.chr} to buff #{buff} "
            else
              #$log.debug "  GOT -1 in escape "
              # it is -1 so evaluate
              x = _evaluate_buff buff
              $key_chr = x if x
              return $key_int if x
              $log.warn "getchar: window.rb 1256 Found no mapping for #{buff} "
              $key_chr = buff
              return $key_int
            end
          end
        end
        
        # what if keyname does not return anything
        if c > 127
          #$log.info "xxxgetchar: window.rb sending #{c} "
=begin
          ch =  FFI::NCurses::keyname(c) 
          # remove those ugly brackets around function keys
          if ch && ch[-1]==')'
            ch = ch.gsub(/[()]/,'')
          end
          if ch && ch.index("KEY_")
            ch = ch.gsub(/KEY_/,'')
          end
          ch = "<#{ch}>" if ch
          #return ch if ch
          $key_chr = ch if ch
          $key_chr = "UNKNOWN:#{c}" unless ch
          $log.warn "getchar: window.rb 1234 Found no mapping for #{c} " unless ch
=end
          #$key_chr = key_tos(ch)
          return c
        end
        if c
          #$key_chr =  c.chr 
          return c 
        end
    end
NOTE: This is a reworked and much simpler version of the original getchar which was taken from manveru's
 codebase. This also currently returns the keycode as int while placing the char version in a
 global $key_chr. Until we are ready to return a char, we use this.

 FIXME : I have tried very hard to revert to nodelay but it does not seem to have an effect when ESC is pressed.
 Somewhere, there is a delay when ESC is pressed. I not longer wish to provide the feature of pressing ESC
 and then a key to be evaluated as Meta-key. This slows down when a user just presses ESC.

 Read a char from the window (from user) and returns int code.
 In some cases, such as codes entered in the $kh hash, we do not yet have a keycode defined
 so we return 9999 and the user can access $key_chr.

 NOTE: Do not convert to string, that is doing two things. Allow user to convert if required using
 `key_tos`