Skip to content

Commit be186c2

Browse files
committed
Update waitKey() to return integer instead of string character
1 parent 56903a4 commit be186c2

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/highgui.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ mp_obj_t cv2_highgui_waitKey(size_t n_args, const mp_obj_t *pos_args, mp_map_t *
8888
// of 0 to wait indefinitely, whereas `select.poll` uses -1
8989
mp_obj_t timeout = MP_OBJ_NEW_SMALL_INT(delay <= 0 ? -1 : delay);
9090

91+
// TODO: Some key presses return multiple characters (eg. up arrow key
92+
// returns 3 characters: "\x1b[A"). Need to handle this case properly.
93+
// Should also look into implementing waitKeyEx() for these extra cases
94+
9195
// Call `poll.poll(timeout)`
9296
mp_obj_t poll_poll_method[3];
9397
mp_load_method(poll_obj, MP_QSTR_poll, poll_poll_method);
@@ -110,5 +114,9 @@ mp_obj_t cv2_highgui_waitKey(size_t n_args, const mp_obj_t *pos_args, mp_map_t *
110114
mp_obj_t read_method[3];
111115
mp_load_method(stdin_obj, MP_QSTR_read, read_method);
112116
read_method[2] = MP_OBJ_NEW_SMALL_INT(1);
113-
return mp_call_method_n_kw(1, 0, read_method);
117+
mp_obj_t key_str = mp_call_method_n_kw(1, 0, read_method);
118+
119+
// Convert the key character to an integer and return it
120+
const char *key_chars = mp_obj_str_get_str(key_str);
121+
return MP_OBJ_NEW_SMALL_INT(key_chars[0]);
114122
}

0 commit comments

Comments
 (0)