often times i need to break out from a loop if user hit the escape key.  in order to do that, will have to:

  1. save the existing state of the set escape
  2. set up on escape
  3. restore set escape afterward

to those not familiar with vfp commands and how vfp does things, it may be difficult to figure this logic out.  i have encapulated all this into a class to make it easier to understand what is happening.

 loesctoabort = createobject('syesctoabort')
 for i = 1 to infinity
    if loesctoabort.isaborted()
       exit
    endif
 endfor
 release loesctoabort

the class is:

define class syesctoabort as relation
  
   hidden icoldescape
   hidden ilabort
   hidden ilescapehit
  
   icoldescape = ""
   ilescapehit = sys(2015)
  
   function init
      this.icoldescape = set("escape")
      set escape on
      public (this.ilescapehit)
      this.reset()
      execscript([on escape ] + this.ilescapehit + [ = .t.])
   endfunc
  
   function destroy
      release (this.ilescapehit)
      lcoldescape = this.icoldescape
      set escape &lcoldescape
      on escape
   endfunc
  
   function isaborted()
      local llaborted
      llaborted = evaluate(this.ilescapehit)
      return llaborted
   endfunc
  
   function reset()
      execscript(this.ilescapehit + [ = .f.])
   endfunc
enddef

note that:

  1. the class creates a global variable during its lifetime.
  2. the original on escape command is not preserved.

 

One Response to A class to encapsulate ON ESCAPE

  • Tom says:

    I think using EXECSCRIPT for one line of code is not a good idea. Execscript must create temporary file on the disk, compile that, … so it is slow. You can use macro instead.

    Assigning value into public variable you can do by

    STORE llValue to (this.ilEscapeHit)

Leave a Reply

Your email address will not be published. Required fields are marked *