Author Topic: Catch Esc Key while reading GRREAD and execute code before exit  (Read 1637 times)

0 Members and 1 Guest are viewing this topic.

mailmaverick

  • Bull Frog
  • Posts: 495
Catch Esc Key while reading GRREAD and execute code before exit
« on: February 23, 2016, 03:52:22 AM »
Hi

How to catch Esc key pressed by user while taking user input by : (grread nil 15 1)
Esc key stops program execution without any error message.

But I want to execute certain lines of code before exit. How to do that ?

kpblc

  • Bull Frog
  • Posts: 396
Re: Catch Esc Key while reading GRREAD and execute code before exit
« Reply #1 on: February 23, 2016, 04:03:02 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun test (/ gr err)
  2.   (setq err (vl-catch-all-apply
  3.               (function
  4.                 (lambda () (while (setq gr (grread nil 15 1)) (princ (strcat "\r" (vl-princ-to-string gr)))))
  5.                 ) ;_ end of function
  6.               ) ;_ end of vl-catch-all-apply
  7.         ) ;_ end of setq
  8.     (alert "Esc pressed!")
  9.     ) ;_ end of if
  10.   ) ;_ end of defun
Sorry for my English.

mailmaverick

  • Bull Frog
  • Posts: 495
Re: Catch Esc Key while reading GRREAD and execute code before exit
« Reply #2 on: February 23, 2016, 05:56:08 AM »
Works like a charm !!!! Thank You !!!!!

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: Catch Esc Key while reading GRREAD and execute code before exit
« Reply #3 on: February 23, 2016, 06:18:53 AM »
Since the user input is received during evaluation of the grread function, only this function needs to be evaluated by the vl-catch-all-apply expression, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / g )
  2.     (princ "\nPress any key to exit...")
  3.     (while
  4.         (and
  5.             (not (vl-catch-all-error-p (setq g (vl-catch-all-apply 'grread '(nil 15 1)))))
  6.             (= 5 (car g))
  7.         )
  8.     )
  9.     (princ "\nUser exited.")
  10.     (princ)
  11. )

That said, perhaps there is a performance consideration in the difference between evaluating a vl-catch-all-error-p/vl-catch-all-apply expression continuously, or catching the error from continuous evaluation of the grread function. So with this reasoning, maybe the solution suggested by kpblc is more efficient.
« Last Edit: February 23, 2016, 06:22:17 AM by Lee Mac »