Author Topic: Detect enter key in dialog EDITBOX field  (Read 2490 times)

cssyphus

  • Newbie
  • *
  • Posts: 14
Detect enter key in dialog EDITBOX field
« on: March 12, 2014, 01:53:41 pm »
I have a fully paid up maintenance plan (ie. 2014A) but am using 2011B at the moment.

Since a WB dialog hits its callback routine multiple times per second, is it possible to detect when user depresses the enter key when typing in a dialog EDITBOX field?

On a single-field entry form, users want to quickly type and press Enter key, without needing to grab mouse to click the OK button

I am accustomed to programming in jQuery/javascript and am looking for something like

Code: Winbatch
$('#inputField').keyup(function(e) {
    var keycode = (e.keyCode ? e.keyCode : e.which);
    if(keycode == '13'){
        //Enter was pressed
    }
});
 

Is something like this possible in WinBatch?

Here is my callback proc:

Code: Winbatch
#DEFINESUBROUTINE MessageUdfProc(dlgMessageUdfHandle,dlgMessageUdfEvent,dlgMessageUdfEventControlID,dlgMessageUdfParam4,dlgMessageUdfParam5)
        switch (dlgMessageUdfEvent)
                case 0 ;dialog initialization event
                        DialogControlState(dlgMessageUdfHandle, 5, 1, 0) ;DCSTATE_SETFOCUS = 1
                        dialogprocoptions(dlgMessageUdfHandle, 2, 1)  ;watch for push button events
                        msgTimeStart = TimeYmdHms()
                        break
                case 1 ;timer event
                        ;**************************************************************
                        ;   TEST IF USER HAS PRESSED ENTER KEY
                        ;**************************************************************
                        msgTimeLeft = Timeout - TimeDiffSecs(TimeYmdHms(),msgTimeStart)
                        dialogcontrolset(dlgMessageUdfHandle,1,4,msgTimeLeft)  ;update timeout display
                        break
                case 2 ;pushbutton event
                        tmpInputText = dialogcontrolget(dlgMessageUdfHandle,5,3)  ;Control#5, 3=GetAllTextCurrentlyInTheControl
                        return 1
                        break
        endswitch
   return -2
#ENDSUBROUTINE ;MessageUdfProc
 

JTaylor

  • Pundit
  • *****
  • Posts: 927
    • Data & Stuff Inc.
Re: Detect enter key in dialog EDITBOX field
« Reply #1 on: March 12, 2014, 02:15:46 pm »
If you set the OK button as the Default control it should act as you wish.   That is found under Style attributes when using the Dialog Editor.

Jim

cssyphus

  • Newbie
  • *
  • Posts: 14
Re: Detect enter key in dialog EDITBOX field
« Reply #2 on: March 12, 2014, 02:37:42 pm »
Underdog to the rescue... again!

Thanks Jim, I'd completely forgotten that setting, which does solve my problem. However, I remain curious about the detection of individual key strokes (esp. Enter key) for another application.

Just now, I found this: "Read Keys from Keyboard" TechDB article ID: W15291

In the past, I remember the GetKeyboardState function being somewhat unreliable and sometimes missing a key or two. However, that is foggy memory and needs current experimentation.

Thanks again.

JTaylor

  • Pundit
  • *****
  • Posts: 927
    • Data & Stuff Inc.
Re: Detect enter key in dialog EDITBOX field
« Reply #3 on: March 12, 2014, 03:48:13 pm »
Thought this was in the TechDB but didn't see it.  This approach works fairly well. 

Jim


Code: Winbatch
GoSub Load_Routines

Call("wb_dialog_variables.wbc","")

KEYPRESSFormat=`WWWDLGED,6.1`

KEYPRESSCaption=`KeyPress`
KEYPRESSX=002
KEYPRESSY=038
KEYPRESSWidth=182
KEYPRESSHeight=081
KEYPRESSNumControls=002
KEYPRESSProcedure=`KeyPress`
KEYPRESSFont=`DEFAULT`
KEYPRESSTextColor=`DEFAULT`
KEYPRESSBackground=`DEFAULT,DEFAULT`
KEYPRESSConfig=0

KEYPRESS001=`067,039,036,012,PUSHBUTTON,DEFAULT,"OK",1,1,32,DEFAULT,DEFAULT,DEFAULT`
KEYPRESS002=`115,039,036,012,PUSHBUTTON,DEFAULT,"Cancel",0,2,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

ButtonPushed=Dialog("KEYPRESS")



:Load_Routines

 

#DefineSubRoutine KeyPress(KEY_Handle,DMsg,DCID,resvd4,resvd5)

Switch (DMsg)
    Case msg_init                 ; Dialog Initialization
    DialogProcOptions(KEY_Handle, msg_timer,10)                            ; TimerEvent (0- Off).
    DialogProcOptions(KEY_Handle, msg_buttonpushed,1)                     ; PushButton/PictureButton.
    a_answer = 0
    pb_KEY_OK                                = 1
    pb_KEY_Cancel                            = 2
    WinTitle("","KeyPress")
    Break
  Case msg_timer                ; TimerEvent
    a_answer = 0
    a_answer = WaitForKeyEx("{F1}       {F2}    {F3}    {F4}    {F5}    {F6}    {F7}    {F8}    {F9}    {F10}   {F11}   {F12}   {INS}   {DEL}",.01)
    Switch(a_answer)
      Case 0
        If DialogProcOptions(KEY_Handle,1,-1) == 10 Then Return -1
        Break
      Case a_answer
        If WinExist("KeyPress") Then win_state = WinState("KeyPress")
        If StrSub(WinGetActive(),1,8) != "KeyPress" Then Return -1   ;Checks to make sure your script is active
                                                                     ;to avoid hotkey conflicts.
    EndSwitch

    Break
  Case msg_buttonpushed         ; PushButtion
    Switch(DCID)
      Case pb_KEY_OK
        nSelection = DialogControlGet(KEY_Handle,pb_KEY_OK,dc_title)
        Exit
        Break
      Case pb_KEY_Cancel
        nSelection = DialogControlGet(KEY_Handle,pb_KEY_Cancel,dc_title)
        Exit
        Break
    EndSwitch
    Break
EndSwitch

    Switch(a_answer)
      Case 0
        Break
      Case 1
        message("HELLO","You pressed F1")
        Break
      Case 2
        message("HELLO","You pressed F2")
       Break
      Case 3
        message("HELLO","You pressed F3")
        Break
      Case 4
        message("HELLO","You pressed F4")
        Break
      Case 5
        message("HELLO","You pressed F5")
        Break
      Case 6
        message("HELLO","You pressed F6")
        Break
      Case 7
        message("HELLO","You pressed F7")
        Break
      Case 8
        message("HELLO","You pressed F8")
        Break
      Case 9
        message("HELLO","You pressed F9")
        Break
      Case 10
        message("HELLO","You pressed F10")
        Break
      Case 11
        message("HELLO","You pressed F11")
        Break
      Case 12
        message("HELLO","You pressed F12")
        Break
      Case 13              ;INSERT
        message("HELLO","You pressed INSERT")
        Break
      Case 14              ;DELETE
        message("HELLO","You pressed DELETE")
        Break
    EndSwitch
    a_answer = 0

Return -2

#EndSubRoutine
 

Return