AddDialogHandler>Dialog1,EDIT3,OnEnter = enter key press

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
krash2501
Newbie
Posts: 15
Joined: Thu Jul 21, 2016 7:04 pm

AddDialogHandler>Dialog1,EDIT3,OnEnter = enter key press

Post by krash2501 » Tue Mar 07, 2017 1:15 am

I want to this to work when someone presses enter on a keyboard in the dialog:

AddDialogHandler>Dialog1,EDIT3,OnEnter,DoClick(1)

but what event name do i pick?

Thanks

User avatar
JRL
Automation Wizard
Posts: 3497
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: AddDialogHandler>Dialog1,EDIT3,OnEnter = enter key press

Post by JRL » Tue Mar 07, 2017 2:13 pm

AFAIK there is no simple way to make an edit object accept an enter key press. This method isn't too complicated. You can use the OnKeyPress event which will check every key press. The result will be a variable that starts with the called subroutine name then an underscore then the word "key". So if your subroutine name is DoClick, the variable will be DoClick_Key. The value of DoClick_Key for an enter key press will be 13.

The OnEnter event has nothing to do with the enter key. It is a trigger for when an object becomes focused.


Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  Left = 429
  Top = 195
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'CustomDialog'
  ClientHeight = 211
  ClientWidth = 476
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 96
  TextHeight = 13
  object Edit1: TEdit
    Left = 36
    Top = 19
    Width = 121
    Height = 21
    TabOrder = 9
    Text = 'Edit1'
  end
  object Edit2: TEdit
    Left = 36
    Top = 49
    Width = 121
    Height = 21
    TabOrder = 10
    Text = 'Edit2'
  end
  object Edit3: TEdit
    Left = 36
    Top = 79
    Width = 121
    Height = 21
    TabOrder = 11
    Text = 'Edit3'
  end
end
EndDialog>Dialog1


AddDialogHandler>Dialog1,Edit1,OnKeyDown,DoClick1
AddDialogHandler>Dialog1,Edit2,OnKeyDown,DoClick2
AddDialogHandler>Dialog1,Edit3,OnKeyDown,DoClick3

Show>Dialog1,


SRT>DoClick1
  If>DoClick1_key=13
    GetDialogProperty>Dialog1,Edit1,Text,vStr1
    MDL>vStr1
  Endif
END>DoClick1

SRT>DoClick2
  If>DoClick2_key=13
    GetDialogProperty>Dialog1,Edit2,Text,vStr2
    MDL>vStr2
  Endif
END>DoClick2

SRT>DoClick3
  If>DoClick3_key=13
    GetDialogProperty>Dialog1,Edit3,Text,vStr3
    MDL>vStr3
  Endif
END>DoClick3

krash2501
Newbie
Posts: 15
Joined: Thu Jul 21, 2016 7:04 pm

Re: AddDialogHandler>Dialog1,EDIT3,OnEnter = enter key press

Post by krash2501 » Tue Mar 07, 2017 5:22 pm

Thanks :)

hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: AddDialogHandler>Dialog1,EDIT3,OnEnter = enter key press

Post by hagchr » Tue Mar 07, 2017 9:02 pm

I had the same problem at one point and never could figure out how to avoid the annoying beeps that comes when you press ENTER in an Edit field. So instead I used a Memo field and removed the line break that comes when you press ENTER. See simple illustration below (I am sure it can be done easier but it shows how I was thinking). Only the Memo field bottom left will trigger a message when ENTER is pressed. The Edit field will just beep and the Memo field on the right will just add line breaks in the text. Hope it helps.

Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  Left = 255
  Top = 117
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'CustomDialog'
  ClientHeight = 211
  ClientWidth = 476
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 70
    Top = 24
    Width = 24
    Height = 13
    Caption = 'Edit1'
  end
  object Label2: TLabel
    Left = 70
    Top = 88
    Width = 51
    Height = 13
    Caption = 'MSMemo1'
  end
  object Label3: TLabel
    Left = 259
    Top = 24
    Width = 51
    Height = 13
    Caption = 'MSMemo2'
  end
  object Edit1: TEdit
    Left = 70
    Top = 41
    Width = 155
    Height = 21
    AutoSelect = False
    TabOrder = 0
    Text = 'ENTER will NOT trigger'
  end
  object MSMemo1: tMSMemo
    Left = 70
    Top = 104
    Width = 155
    Height = 25
    Text = 'ENTER will trigger'
    TabOrder = 1
  end
  object MSButton1: tMSButton
    Left = 259
    Top = 147
    Width = 75
    Height = 25
    Caption = 'Exit'
    TabOrder = 3
    DoBrowse = False
    BrowseStyle = fbOpen
  end
  object MSMemo2: tMSMemo
    Left = 259
    Top = 40
    Width = 185
    Height = 89
    Text = 'ENTER will not Trigger'#13#10
    WordWrap = True
    TabOrder = 2
  end
end
EndDialog>Dialog1
Show>Dialog1

//Add Handlers for entering and exiting MSMemo1, and for pressing EXIT button
AddDialogHandler>Dialog1,msMemo1,onEnter,DoActivateTrigger
AddDialogHandler>Dialog1,msMemo1,onExit,DoDeactivateTrigger
AddDialogHandler>Dialog1,msButton1,onClick,DoExit

//When ENTER is pressed then DoAction (status of TriggerFlag will determine if anything will happen or not).
//TriggerFlag is TRUE when focus is on MSMemo1, FALSE otherwise
Let>TriggerFlag=FALSE
OnEvent>KEY_DOWN,VK13,0,DoAction

//Main Loop
//==================================================
Label>Main
  Wait>0,1
Goto>Main
//==================================================

SRT>DoAction
  //TriggerFlag is TRUE if focus is on MSMemo1 
  If>TriggerFlag=TRUE
    //Turn off the onEvent for ENTER press
    OnEvent>KEY_DOWN,VK13,0,
    //Get the text in MSMemo1 into var tmpVar
    GetDialogProperty>Dialog1,MSMemo1,Text,tmpVar
    //Get the first line of the text into variable m_1. NB when ENTER was pressed a LineBreak was added which is removed here.
    RegEx>(?m)^.*(?=\R),tmpVar,0,m,nm,0
    //Put the first line back into MSMemo1
    SetDialogProperty>Dialog1,MSMemo1,Text,m_1
    //Press END to ensure cursor will be at the end of the text field
    Press>END
    //Message with contents of MSMemo1
    MDL>m_1
    
    //Add a wait to reduce risk that pressing ENTER to close MDL will trigger the onEvent again. 
    Wait>0.25
    
    //Turn on again the onEvent for ENTER press
    OnEvent>KEY_DOWN,VK13,0,DoAction
  EndIf
END>DoAction

//Activate ENTER trigger, ie set TriggerFlag to TRUE
SRT>DoActivateTrigger
  Press>END
  Let>TriggerFlag=TRUE
END>DoActivateTrigger

//Deactivate ENTER trigger, ie set TriggerFlag to FALSE
SRT>DoDeactivateTrigger
  Let>TriggerFlag=FALSE
END>DoDeactivateTrigger

//Exit
SRT>DoExit
    Exit>res
END>DoExit

zabros2020
Pro Scripter
Posts: 70
Joined: Sun May 03, 2009 11:49 pm
Location: AU

Re: AddDialogHandler>Dialog1,EDIT3,OnEnter = enter key press

Post by zabros2020 » Fri Mar 10, 2017 5:52 am

krash2501 wrote:I want to this to work when someone presses enter on a keyboard in the dialog:

AddDialogHandler>Dialog1,EDIT3,OnEnter,DoClick(1)

but what event name do i pick?

Thanks
Hi Krash,
You can try this also....works for text boxes

Code: Select all


Dialog>Dialog1
object Dialog1: TForm
  Left = 902
  Top = 104
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'CustomDialog'
  ClientHeight = 140
  ClientWidth = 146
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  Position = poScreenCenter
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 96
  TextHeight = 13
  object txtText: TEdit
    Left = 9
    Top = 9
    Width = 121
    Height = 21
    TabOrder = 0
    Text = 'Press ENTER to trigger'
  end
  object btnExit: tMSButton
    Left = 36
    Top = 55
    Width = 75
    Height = 25
    Caption = 'Exit'
    TabOrder = 1
    DoBrowse = False
    BrowseStyle = fbOpen
  end
end
EndDialog>Dialog1

GoSub>LoadHandlers
Show>Dialog1,

SRT>LoadHandlers
  AddDialogHandler>Dialog1,txtText,OnChange,GetText(0)
  AddDialogHandler>Dialog1,txtText,OnKeyPress,GetText(1)
  AddDialogHandler>Dialog1,btnExit,OnClick,Exit
END>LoadHandlers

SRT>GetText
  'get the text from the text box
  GetDialogProperty>Dialog1,txtText,Text,sText
  'When a user presses a key, determine if it is Enter key(13)
  If>GetText_Var_1=1
    'When enter is pressed do what needs to be done
    If>GetText_Key=13
      MessageModal>ENTER key was pressed
      Let>GetText_Var_1=0
      Let>GetText_Key=
    EndIf
  EndIf
END>GetText

SRT>Exit
  Exit>
END>Exit
Loving MS's Capabilities!!!

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts