Skip to content

more fixes - #39

Merged
GiorgosXou merged 14 commits into
unicurses:masterfrom
ttoommxx:master
Apr 4, 2024
Merged

GiorgosXou merged 14 commits into
unicurses:masterfrom
ttoommxx:master

Conversation

@ttoommxx

@ttoommxx ttoommxx commented Apr 2, 2024

Copy link
Copy Markdown
Contributor

I fixed some of the functions that were taking "ch" as character argument but were missing to apply CCHAR before then.

Some other fixes here and there, most notably now when getch returns -1 the library raise an error. Upon inspection, it looks like -1 is the return code for error when delay is enabled, and as such I thought Python should return an actual TimeoutError. This is clearly up to debate, but previously the library would simply throw away the input, which I am not extremely sure it's the actual wanted behavior. I might be completely wrong here.

@GiorgosXou

GiorgosXou commented Apr 4, 2024 •

Copy link
Copy Markdown
Member

(done) wgetch and mvwgetch now return error if the return key = -1. Upon inspecting halfdelay, it looks like -1 corresponds to the value for error when waiting time

Note that -1 is more like a "fault" rather than an "error" (even though, it's constant ERR, is named after the word error). Take for example this theoritical example:

stdscr = initscr()
nodelay(stdscr, True)
noecho()
cbreak()

while True:
    ch = getch()
    if ch != ERR: # comment this and see what will happen
        print(ch) # ignore that i use stdout here  for a momment :P

When nodelay(...) is True, ch is always -1 until it captures a key. That's the main purpose of it, to have no delay and so i reverted the change.

@GiorgosXou
GiorgosXou merged commit 8817728 into unicurses:master Apr 4, 2024
@ttoommxx

ttoommxx commented Apr 4, 2024

Copy link
Copy Markdown
Contributor Author

oh, I did not know that! My mistake, will investigate more next time before making such big change

@GiorgosXou

GiorgosXou commented Jul 5, 2024 •

Copy link
Copy Markdown
Member

For exampe, here's a use case for capturing escape-sequences\alt-keys, thanks to nodelay(stdscr, True) 😅

def ALT_CONDITION(event, is_default_nodelay=False): # https://stackoverflow.com/a/16248956/11465149
"""
Checks if escape-sequence is an alt-key combination and if so, returns a cross-compatible alt-value of the original event. Assumes you have `cbreak()`, `nodelay(stdscr, ...)` and that you will use it once per `getch`/`get_wch` *(for cross-compatibility)*. See also `set_escdelay()`. This is a custom UNI-CURSES function
"""
if event == 27: # Esc or Alt
nodelay(stdscr, True)
ch = getch() # why not get_wch?
nodelay(stdscr, is_default_nodelay)
if ch == -1: # Escape was pressed
return 27
return ch + 320 # shifting event 320 because that's what pdcurses returns for escape\alt events (plus it doesn't seem to result on anything too weird [at least if you use it properly and in the right order])
return event

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants