Search code examples
delphiradio-grouponkeypress

how to respond to a key press when focused on a radiogroup in delphi


In delphi: How can you respond to a key press when the current focus is on a radiogroup which does not have an onkeypress event. I was hoping to use the forms onkeypress event but it doesnt see to fire.


Solution

  • You can make this possible by setting the form's KeyPreview property to True.

    However, I'm not sure you are actually doing things right, since this is a fairly uncommon problem.

    You didn't write what keyboard shortcut you want to respond to. But please remember that

    • Letters are used to navigate the GUI. For instance, pressing A might select the &All radio button or click the &Add push button. Similarly, Alt+A does the same if the current control allows character input, allows you to open the &Add-ons menu item, etc.

    • If you want to add a proper shortcut like Ctrl+O, it is much better to use a TActionList with an action having this shortcut. This action can be mapped to menu items, buttons, etc., or simply exist in the background not being attached to any visual control. In very simple applications, you might want to use a stand-alone menu item with such a shortcut instead.