DearPyGui icon indicating copy to clipboard operation
DearPyGui copied to clipboard

dpg.add_key_press_handler doesn't fire for Numpad Enter

Open mrmaffen opened this issue 1 year ago • 0 comments

Version of Dear PyGui

Version: 2.0.0 Operating System: Windows 10

My Issue/Question

onEnterPressed is only called when pressing the Return-button. Using Enter on the numpad doesn't seem to work. I have looked for a way to bind another handler to the Enter button but could only find mvKey_Return in the list. Shouldn't mvKey_Return refer to both "Return" and "Enter" anyways? I'm pretty sure that's how it worked in the past, but I'm not sure what changed tbh.

My current workaround as suggested by v-ein on Discord: I'm using 627 as a hardcoded key code for my KeypadEnter callback.

dpg.add_key_press_handler(627, callback=onEnterPressed) # hardcode workaround for missing ImGuiKey_KeypadEnter

Here's @v-ein's comment since the explanation he gave is relevant for this issue:

v-ein — Yesterday at 22:34 For some reason DPG does not export the ImGui constant for this key (ImGuiKey_KeypadEnter) - not sure why, maybe there were portability issues... You can hardcode it yourself but this will break in a future version of DPG:

mvKey_NumPadEnter = 627 # the value obtained from key_press_handler

Alternatively, you can build your own DPG and add the corresponding constant to src/mvContext.cpp.

Or you can open an issue on GitHub and see if somebody checks if this constant leads to any unanticipated issues, and adds it if everything is fine.

Proposed solution therefore would be to either:

  1. make ImGuiKey_KeypadEnter accessible and bind it to "mvKey_NumPadEnter"
  2. mvKey_NumPadEnter = 627 # hardcode it and ask the gods if they intend to make this break in a horrible and insidious way

To Reproduce

Run the test script, try pressing Return and Enter

Expected behavior

Both buttons should lead to the callback function "onEnterPressed" being called

Standalone, minimal, complete and verifiable example

# Here's some code anyone can copy and paste to reproduce your issue
import dearpygui.dearpygui as dpg

def onEnterPressed():
  print ("onEnterPressed")

dpg.create_context()
dpg.create_viewport(title='Custom Title', width=600, height=300)

with dpg.handler_registry():
    dpg.add_key_press_handler(dpg.mvKey_Return, callback=onEnterPressed)

dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

mrmaffen avatar Dec 30 '24 02:12 mrmaffen