Determine which widget is currently focused

27 views
Skip to first unread message

roger tunnicliffe

unread,
Mar 16, 2023, 3:37:12 AM3/16/23
to fltk.general
This seems like it should be simple but I can't find anything that shows clearly how to do this.

I simply wish to know which field the cursor currently sits at:-

If anyone has a code snippet It would save me another day of searching.

Thx
Roger

 

Greg Ercolano

unread,
Mar 16, 2023, 3:44:51 AM3/16/23
to fltkg...@googlegroups.com

    Fl::focus() returns the widget currently in focus.
    If no widget is in focus, Fl::focus() returns NULL.

    Docs for Fl::focus() are here:
    https://www.fltk.org/doc-1.4/group__fl__events.html#ga7f0e9836db43268979e0b3a999583b7f

    Some widgets can have focus but not have a "cursor", like buttons and tabs.

    If you're new to FLTK, read through the first sections of the "FLTK Programming Manual"
    which should cover things like focus in the "Handling Events" section.

roger tunnicliffe

unread,
Mar 16, 2023, 6:20:56 PM3/16/23
to fltk.general
Thanks for the response. Fl_Focus is what I have been using but cannot get it to work. My code is:-
(It acknowledges FunctionKey 2 and supplies an account search where someone can select an account for the Fl_Value_Input field win4_coa_key_acctno)

int eventHandler(int e, Fl_Window *w)
{
if ( e == FL_KEYDOWN )
{
  int k = Fl::event_key();

if ((k==65471)||(k==65507))
{
Fl_Widget* rFocus = Fl::focus();
if (rFocus==win4_coa_key_acctno)
{
saveWindowNo=windowNo;
windowNo=99;
WIN99_ALL_ACCOUNTS();
}
}

Can you see what I have done wrong..

roger tunnicliffe

unread,
Mar 16, 2023, 6:21:00 PM3/16/23
to fltk.general
Apologies if this is a double post (I do have trouble with this forum)

Thanks for the reply. Fl::focus is what I have been looking at but cannot quite get it right.

My Code:

int eventHandler(int e, Fl_Window *w)
{
if ( e == FL_KEYDOWN )
{
  int k = Fl::event_key();

if ((k==65471)||(k==65507))
{
r=Fl::focus();
if (r==win4_coa_key_acctno)
{
saveWindowNo=windowNo;
windowNo=99;
WIN99_ALL_ACCOUNTS();
}
}

It is supposed to trap FunctionKey 2, run an account search and populate win4_coa_key_acctno (Fl_Value_Input)
Can you see what I have wrong ?
Cheers

Matthias Melcher

unread,
Mar 16, 2023, 7:13:36 PM3/16/23
to fltk.general
How is you function called? It's not part of a window class. 

Maybe `void Fl::add_handler(Fl_Event_Handler ha)` can solve your issue. Your event handler is called for `FL_SHORTCUT events that are not recognized by any widget. This lets you provide global shortcut keys.`. Then watch for FL_SHORTCUT events and set a breakpoint to check what Fl::focus() returns.

Greg Ercolano

unread,
Mar 16, 2023, 8:51:38 PM3/16/23
to fltkg...@googlegroups.com

On 3/16/23 14:39, roger tunnicliffe wrote:

Thanks for the response. Fl_Focus is what I have been using but cannot get it to work. My code is:-
(It acknowledges FunctionKey 2 and supplies an account search where someone can select an account for the Fl_Value_Input field win4_coa_key_acctno)

int eventHandler(int e, Fl_Window *w)
{
if ( e == FL_KEYDOWN )
{
  int k = Fl::event_key();

if ((k==65471)||(k==65507))
{
Fl_Widget* rFocus = Fl::focus();
if (rFocus==win4_coa_key_acctno)
{
saveWindowNo=windowNo;
windowNo=99;
WIN99_ALL_ACCOUNTS();
}
}

    ..as Matt said, how is this code being called?

    Add some printf() statements at each level of the if() statements
    to show the code is being called.

    Also, might want to comment what those magic numbers are (65471, 65507)
    to help us out here on the forum, as we're likely not gonna know which keys those are.

    It's probably best to stick with the macro names defined in FL/Enumerations.H,
    e.g. FL_Control_L, etc.



roger tunnicliffe

unread,
Mar 17, 2023, 3:55:46 AM3/17/23
to fltk.general
1) The code is not being called by me, it's an event handler..ie 
Fl::event_dispatch(eventHandler);

2) Everything follows the correct flow as it was run under gdb/ddd

3) Those magic numbers are the values indicated by FLTK. I havent used macro names because keyboards on ChromeOS
are a little different so 65471 is what FLTK returns when FunctionKey 2 is pressed.

Thx
  

Ian MacArthur

unread,
Mar 17, 2023, 5:08:07 AM3/17/23
to fltk.general
On Friday, 17 March 2023 at 07:55:46 UTC roger wrote:
On Friday, March 17, 2023 at 11:51:38 AM UTC+11 Greg wrote:

    Also, might want to comment what those magic numbers are (65471, 65507)

    to help us out here on the forum, as we're likely not gonna know which keys those are.

    It's probably best to stick with the macro names defined in FL/Enumerations.H,
    e.g. FL_Control_L, etc.

 Everything follows the correct flow as it was run under gdb/ddd


3) Those magic numbers are the values indicated by FLTK. I havent used macro names because keyboards on ChromeOS
are a little different so 65471 is what FLTK returns when FunctionKey 2 is pressed.


OK, I don't know much about the focus issue, but this bit (the key codes) I do know.

I don't believe ChromeOS is much different from any other "*nix" in this regard, the stock FLTK enumerations ought to Just Work.

The magic numbers are:

65471 - i.e. 0xFFBF which is  "FL_F + 2"  (0xffbd + 2)  that in turn maps to Fn2

65507 - i.e. 0xFFE3 which maps to FL_Control_L


Reply all
Reply to author
Forward
0 new messages