johnnovak / illwill

A curses inspired simple cross-platform console library for Nim
Do What The F*ck You Want To Public License
398 stars 27 forks source link

getKey() is slow getting mouse input #42

Open xjzh123 opened 9 months ago

xjzh123 commented 9 months ago

I don't know whether this is only for me or everyone on Windows, but I think it is probably a problem that affects many users. And, its performance drawback is vary apparent to me. I am using Windows 11, and it seems that illwill only tries to get mouse input in getKey() when it is on Windows. However, this is making getKey() apparently slow on my computer, and not suitable for making keyboard inputs. Behavior: Maybe the video. (In a nut, very slow and laggy.) Solution: Maybe only try to get mouse input when gMouse is on?

proc getKey*(): Key =
  ## Reads the next keystroke in a non-blocking manner. If there are no
  ## keypress events in the buffer, `Key.None` is returned.
  ##
  ## If a mouse event was captured, `Key.Mouse` is returned. Call `getMouse()`
  ## to get tne details about the event.
  ##
  ## If the module is not intialised, `IllwillError` is raised.
  checkInit()
  result = getKeyAsync()
  when defined(windows):
    if gMouse and result == Key.None:
      if hasMouseInput():
        return Key.Mouse

When I do this edit on my local illwill, it makes my test program much faster.

Video: https://www.veed.io/view/d26c1418-680f-427b-9904-8c3e0d9eec34


When I force import getKeyAsync(), it is much faster too.

from illwill {.all.} import getKeyAsync, Key

while true:
  let k = getKeyAsync()
  if k != Key.None:
    echo k
johnnovak commented 9 months ago

Interesting. I'm not working actively on the library, but feel free to raise a PR.