From a96083c7d156f21ab4b347c7a145bd3b0213d1b0 Mon Sep 17 00:00:00 2001 From: diegohce Date: Wed, 14 Aug 2024 14:47:56 -0300 Subject: [PATCH] added window pollkey function --- highgui.cpp | 4 ++++ highgui.go | 19 +++++++++++++++++++ highgui_gocv.h | 1 + highgui_test.go | 11 +++++++++++ 4 files changed, 35 insertions(+) diff --git a/highgui.cpp b/highgui.cpp index 87177131..0e0c812b 100644 --- a/highgui.cpp +++ b/highgui.cpp @@ -29,6 +29,10 @@ int Window_WaitKey(int delay = 0) { return cv::waitKey(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 a32e1cd9..b3e4245d 100644 --- a/highgui.go +++ b/highgui.go @@ -162,6 +162,25 @@ func (w *Window) WaitKey(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 d1bd33b5..6a12e86e 100644 --- a/highgui_gocv.h +++ b/highgui_gocv.h @@ -16,6 +16,7 @@ double Window_GetProperty(const char* winname, int flag); 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_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 bb69dd7e..a5f3554b 100644 --- a/highgui_test.go +++ b/highgui_test.go @@ -112,3 +112,14 @@ func TestTrackbarWithValue(t *testing.T) { t.Error("Trackbar pos should have been 50") } } + +func TestPollKey(t *testing.T) { + + w := NewWindow("polly") + defer w.Close() + + if v := w.PollKey(); v != -1 { + t.Errorf("got %d want -1", v) + } + +}