1

I'm trying to build a UI that responds to different keys.

while True:
  k = cv2.waitKey(1)  # I get the same results using waitKeyEx
  if k == -1:
    continue
  if k == 27 or k == ord('q'):  # 27 is the Escape key
    cv2.destroyAllWindows()
    return
  if k == 113:
    print('f2 hit')

My problem is, hitting the 'q' button and hitting my 'f2' button both create a k value of 113.

Is there any way to distinguish between these two keys?

Is there an equivalent for the ord('q') call that can be used with f-keys?

Opencv 4.5.3
Windows 10

  GUI: 
    QT:                          YES (ver 5.12.9)
      QT OpenGL support:         NO
ahbutfore
  • 399
  • 4
  • 10
  • What version of OpenCV? What OS? Which HighGUI backend? – Dan Mašek Oct 22 '21 at 17:35
  • 1
    Opencv 4.5.3, Windows 10. I'm not really clear on what exactly HighGUI's are. I installed opencv with "conda install -c conda-forge opencv" and the only thing I think could maybe affect things is also later using "pip install pyqt5". – ahbutfore Oct 22 '21 at 17:42
  • 1
    Add that info to your question. For the HighGUI backend, do `print(cv2.getBuildInformation())` and look at the GUI section of the output. | Using a standard Windows build on Win10, I can't reproduce this -- if I hit F2 then `waitKey` returns 0, and `waitKeyEx` returns 7405568. – Dan Mašek Oct 22 '21 at 17:55
  • HighGUI is the OpenCV module implementing the high-level GUI functionality that you're trying to use: https://docs.opencv.org/4.5.3/d7/dfc/group__highgui.html – Dan Mašek Oct 22 '21 at 17:59
  • 1
    Added to question, thanks. – ahbutfore Oct 22 '21 at 18:13
  • 2
    Afair you need a bitwise-and with some value to get the actial key in waitKeyEx, because waitKeyEx uses a higher state for additional information. – Micka Oct 22 '21 at 18:20
  • @Micka I have a feeling there's something wonky about this in the QT backend. Might be worth filing a bug. – Dan Mašek Oct 22 '21 at 18:23
  • 1
    According to the docs, waitKeyEx behaviour ist backend-dependant. However is there still in a chance in the bitwise-and? https://stackoverflow.com/a/68155662/2393191 – Micka Oct 22 '21 at 18:28
  • waitKey and waitKeyEx both return 113 for both q and F2 keys with no bitwise-ands. – ahbutfore Oct 22 '21 at 18:53
  • 7405568 mod 255 is 113, so either waitKey or your comparison operator seems to take modulo of the 32 bit k – Micka Oct 22 '21 at 19:15
  • What's the return type format of waitKey in python? Is it something special like numpy.int? – Micka Oct 22 '21 at 19:21
  • 1
    it's a regular python int, which is a bigint, i.e. arbitrary bit length. no, waitKeyEx should *return* all the information. if it doesn't, something's fishy and my bet is on **Qt** being to blame. – Christoph Rackwitz Oct 22 '21 at 22:34
  • 2
    @ChristophRackwitz Looking at the source code, it seems to me that for QT backend a call to `waitKeyEx` falls back to `waitKey`. There seems to be some turmoil happening in the HighGUI module, and for one QT doesn't have a `UIBackend` implementation for it. So in this case I'd expect both functions to return the same result. And while the Qt backend might be able to return extended keycodes (it seems there are provisions for that), either there's something I haven't noticed masking those out, or it just doesn't work well on Windows. – Dan Mašek Oct 24 '21 at 00:59
  • 1
    so... bug in OpenCV means someone should look for the issue and create if it doesn't exist https://github.com/opencv/opencv/issues perfect opportunity for OP to get their feet wet – Christoph Rackwitz Oct 24 '21 at 11:16
  • @DanMašek Ah an issue like that would certainly explain this behavior. So to fix this in the short term, is there some simple way of swapping out the gui backend used in opencv or is that something baked into the opencv pip install and I'd have to like go build opencv myself to change it? – ahbutfore Oct 25 '21 at 02:38
  • The backend is selected at build time, so you need a different build. Since you're on Windows, you could use the WinAPI backend. The standard Windows release from https://github.com/opencv/opencv/releases contains Python libraries as well. That's what I generally use and install it manually (mostly since I have some 10+ different versions installed in parallel, to facilitate answering questions here :D ). – Dan Mašek Oct 25 '21 at 03:00

0 Answers0