diff --git a/highgui.cpp b/highgui.cpp index 277ab368..0cedbc97 100644 --- a/highgui.cpp +++ b/highgui.cpp @@ -33,6 +33,10 @@ int Window_WaitKeyEx(int delay = 0) { return cv::waitKeyEx(delay); } +int Window_PollKey(void) { + return cv::pollKey(); +} + void Window_Move(const char* winname, int x, int y) { cv::moveWindow(winname, x, y); } diff --git a/highgui.go b/highgui.go index 763fa1a8..b0b96e19 100644 --- a/highgui.go +++ b/highgui.go @@ -172,6 +172,25 @@ func (w *Window) WaitKeyEx(delay int) int { return int(C.Window_WaitKey(C.int(delay))) } +// PollKey polls for a pressed key. +// The function pollKey polls for a key event without waiting. +// It returns the code of the pressed key or -1 if no key was pressed since +// the last invocation. To wait until a key was pressed, use waitKey. +// +// The functions waitKey and pollKey are the only methods in HighGUI that can +// fetch and handle GUI events, so one of them needs to be called periodically +// for normal event processing unless HighGUI is used within an environment that +// takes care of event processing. +// The function only works if there is at least one HighGUI window created and +// the window is active. If there are several HighGUI windows, any of them can +// be active. +// +// For further details, please see: +// https://docs.opencv.org/4.x/d7/dfc/group__highgui.html#ga6d20fbd3100ec3badc1eaa653aff99d7 +func (w *Window) PollKey() int { + return int(C.Window_PollKey()) +} + // MoveWindow moves window to the specified position. // // For further details, please see: diff --git a/highgui_gocv.h b/highgui_gocv.h index 47abb65b..113250cf 100644 --- a/highgui_gocv.h +++ b/highgui_gocv.h @@ -17,6 +17,7 @@ void Window_SetProperty(const char* winname, int flag, double value); void Window_SetTitle(const char* winname, const char* title); int Window_WaitKey(int); int Window_WaitKeyEx(int); +int Window_PollKey(void); void Window_Move(const char* winname, int x, int y); void Window_Resize(const char* winname, int width, int height); struct Rect Window_SelectROI(const char* winname, Mat img); diff --git a/highgui_test.go b/highgui_test.go index aa26b155..73016ba3 100644 --- a/highgui_test.go +++ b/highgui_test.go @@ -113,6 +113,16 @@ func TestTrackbarWithValue(t *testing.T) { } } +func TestPollKey(t *testing.T) { + + w := NewWindow("polly") + defer w.Close() + + if v := w.PollKey(); v != -1 { + t.Errorf("got %d want -1", v) + } +} + func TestWaitKeyEx(t *testing.T) { w := NewWindow("wait")