October 30, 2025, 05:21:23 PM

News:

IWBasic runs in Windows 11!


Detecting enter key on a button

Started by Andy, January 29, 2012, 02:17:58 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andy

Hi,

Propably a simple question but I can't see why this doesn't work
I'm trying to detect when the enter / tab key is pressed for a button.

WINDOW win
OPENWINDOW win,0,0,295,250,0,0,"Caption",&handler
CONTROL win,@EDIT,"Press Enter or TAB",83,39,124,27,0x50800000|@CTEDITAUTOH,5
CONTROL win,@EDIT,"Press Enter or TAB",83,100,124,27,0x50800000|@CTEDITAUTOH,6
CONTROL win,@BUTTON,"OK",83,140,100,27,@TABSTOP,7
SETFONT win,"Ariel",9,400,0,5
SETCONTROLNOTIFY(win,5,1,1)
SETCONTROLNOTIFY(win,6,1,1)
SETCONTROLNOTIFY(win,7,1,1)
SETFOCUS win,5
WAITUNTIL win=0
END

SUB handler
SELECT @MESSAGE
   CASE @IDCONTROL
      IF @CONTROLID = 5
         SELECT @NOTIFYCODE
            CASE @ENENTERKEY
               MESSAGEBOX win,GETCONTROLTEXT(win,5),"Enter Pressed!"
               SETFOCUS win,6
            CASE @ENTABKEY
               MESSAGEBOX win,"Tab key pressed","Info"
               SETFOCUS win,6
         ENDSELECT
      ENDIF

      IF @CONTROLID = 6
         SELECT @NOTIFYCODE
            CASE @ENENTERKEY
               'MESSAGEBOX win,GETCONTROLTEXT(win,6),"Enter Pressed!"
               SETFOCUS win,7
            CASE @ENTABKEY
               MESSAGEBOX win,"Tab key pressed","Info"
               SETFOCUS win,7
         ENDSELECT
      ENDIF

      IF @CONTROLID = 7
         SELECT @NOTIFYCODE
            CASE @ENENTERKEY
               MESSAGEBOX win,GETCONTROLTEXT(win,7),"Enter Pressed!"
               SETFOCUS win,7
            CASE @ENTABKEY
               MESSAGEBOX win,"Tab key pressed","Info"
               SETFOCUS win,7
         ENDSELECT
      ENDIF


   CASE @IDCREATE
      CENTERWINDOW win
   CASE @IDCLOSEWINDOW
      CLOSEWINDOW win
ENDSELECT
RETURN 0
ENDSUB

If I change

CONTROL win,@BUTTON,"OK",83,140,100,27,@TABSTOP,7

with

CONTROL win,@CHECKBOX,"OK",83,140,100,27,@TABSTOP,7

it works, any ideas how to modify this so the button detects the enter key?

Thanks,
Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

aurelCB

Hi Andy ...
Tabstop don't have efect on window...
Do you mean something like this:
WINDOW win
INT focus5,focus6,focus7
OPENWINDOW win,0,0,295,250,0,0,"Caption",&handler
CONTROL win,@EDIT,"Press Enter or TAB",83,39,124,27,0x50800000|@CTEDITAUTOH|@tabstop,5
CONTROL win,@EDIT,"Press Enter or TAB",83,100,124,27,0x50800000|@CTEDITAUTOH|@tabstop,6
CONTROL win,@BUTTON,"OK",83,140,150,27,@tabstop,7
SETFONT win,"Ariel",9,400,0,5
SETCONTROLNOTIFY(win,5,1,1)
SETCONTROLNOTIFY(win,6,1,1)
SETCONTROLNOTIFY(win,7,1,1)
'SETFOCUS win,5
WAITUNTIL win=0
END

SUB handler
SELECT @MESSAGE
   CASE @IDCONTROL

      IF @CONTROLID = 5
         SELECT @NOTIFYCODE
            CASE @ENENTERKEY
               SETCONTROLCOLOR win,7,rgb(200,200,250),rgb(80,80,100)
SETCONTROLTEXT win,7,"ENTER Pressed!"
            CASE @ENTABKEY
SETCONTROLCOLOR win,7,rgb(200,200,0),rgb(0,0,0)
SETCONTROLTEXT win,7,"TAB Pressed!"
setfocus win,6:focus5=0:focus6=1:focus7=0
IF focus6=1
SETCONTROLCOLOR win,6,rgb(0,0,0),rgb(240,240,230)
SETCONTROLCOLOR win,5,rgb(0,0,0),rgb(255,255,255)
ENDIF
         ENDSELECT
      ENDIF

      IF @CONTROLID = 6
         SELECT @NOTIFYCODE
            CASE @ENENTERKEY
               SETCONTROLCOLOR win,7,rgb(200,200,250),rgb(80,80,100)
SETCONTROLTEXT win,7,"ENTER Pressed!"
            CASE @ENTABKEY
SETCONTROLCOLOR win,7,rgb(200,200,0),rgb(0,0,0)
SETCONTROLTEXT win,7,"TAB Pressed!"
               setfocus win,5:focus5=1:focus6=0:focus7=0
IF focus5=1
SETCONTROLCOLOR win,5,rgb(0,0,0),rgb(240,240,230)
SETCONTROLCOLOR win,6,rgb(0,0,0),rgb(255,255,255)
ENDIF
         ENDSELECT
      ENDIF

      IF @CONTROLID = 7
         
      ENDIF


   CASE @IDCREATE
      CENTERWINDOW win
   CASE @IDCLOSEWINDOW
      CLOSEWINDOW win
ENDSELECT
RETURN 0
ENDSUB

Andy

Hi,

Thanks for trying, i'm after detecting the enter key press only when the focus is set to the button.

In the example posted the enter key is pressed and the focus moves to the next edit box and so on, but when the focus moves to the button I cannot detect the enter key whilst the focus is on the button.

Thanks for trying!  :)
Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

ckoehn

This is how you enable TABSTOPS.  Not sure about ENTER on a Button control.

WINDOW win
OPENWINDOW win,0,0,295,250,0,0,"Caption",&handler
CONTROL win,@EDIT,"Press Enter or TAB",83,39,124,27,0x50800000|@CTEDITAUTOH|@TABSTOP,5 'added @TABSTOP
CONTROL win,@EDIT,"Press Enter or TAB",83,100,124,27,0x50800000|@CTEDITAUTOH|@TABSTOP,6 'added @TABSTOP
CONTROL win,@BUTTON,"OK",83,140,100,27,@TABSTOP,7
ENABLETABS win,1 'added this
SETFONT win,"Ariel",9,400,0,5
SETCONTROLNOTIFY(win,5,1,1)
SETCONTROLNOTIFY(win,6,1,1)
SETCONTROLNOTIFY(win,7,1,1)
SETFOCUS win,5
WAITUNTIL win=0
END

SUB handler
SELECT @MESSAGE
   CASE @IDCONTROL
      IF @CONTROLID = 5
         SELECT @NOTIFYCODE
            CASE @ENENTERKEY
               MESSAGEBOX win,GETCONTROLTEXT(win,5),"Enter Pressed!"
               SETFOCUS win,6
            CASE @ENTABKEY
               MESSAGEBOX win,"Tab key pressed","Info"
               SETFOCUS win,6
         ENDSELECT
      ENDIF

      IF @CONTROLID = 6
         SELECT @NOTIFYCODE
            CASE @ENENTERKEY
               'MESSAGEBOX win,GETCONTROLTEXT(win,6),"Enter Pressed!"
               SETFOCUS win,7
            CASE @ENTABKEY
               MESSAGEBOX win,"Tab key pressed","Info"
               SETFOCUS win,7
         ENDSELECT
      ENDIF

      IF @CONTROLID = 7
         SELECT @NOTIFYCODE
            CASE @ENENTERKEY
               MESSAGEBOX win,GETCONTROLTEXT(win,7),"Enter Pressed!"
               SETFOCUS win,7
            CASE @ENTABKEY
               MESSAGEBOX win,"Tab key pressed","Info"
               SETFOCUS win,7
         ENDSELECT
      ENDIF


   CASE @IDCREATE
      CENTERWINDOW win
   CASE @IDCLOSEWINDOW
      CLOSEWINDOW win
ENDSELECT
RETURN 0
ENDSUB


Later,
Clint

Andy

Clint,

Thanks for having a look and trying, i'm ok with tabbing the @tabstop was just included because I copied the entire line from another program, no other reason.

Yes, it's the enter key on a button control I'm trying to find, no luck yet!

Thanks,
Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

Andy


Hi,

Problem solved!!

Well i've found away to detect the Enter key for a button, to test it just press Enter when the 'OK' button changes to black.

Hope this helps everyone! :)
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.