Author Topic: react like enter when press space bar with getsting function  (Read 3349 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
react like enter when press space bar with getsting function
« on: February 21, 2017, 05:41:33 AM »
Hello guys.

I am looking for a way to end the function (getstring t) when I press space bar as the first char instead of enter.
is there any way to solve this?

Thanks in advance.

Code: [Select]
(or *myString* (setq *myString* "Pile-01"))

(setq *myString* (getstring t (strcat "Name of pile < " *myString* " > :")))

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: react like enter when press space bar with getsting function
« Reply #1 on: February 21, 2017, 05:48:37 AM »
Tharwat helped me here.
The whole idea was to construct a list of strings, however his suggestion concatenates the inputed text into one big string.
But in that thread I was reffering about creating an MTEXT entity, IDK whats your intention here.
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

Coder

  • Swamp Rat
  • Posts: 827
Re: react like enter when press space bar with getsting function
« Reply #2 on: February 21, 2017, 06:02:27 AM »
Tharwat helped me here.
The whole idea was to construct a list of strings, however his suggestion concatenates the inputed text into one big string.
But in that thread I was reffering about creating an MTEXT entity, IDK whats your intention here.

Thank you for your help.

I think what I am after is something else. sorry.

the function getstring with t allows you to write a text with space then I want to end the input by pressing Space bar as if I am pressing Enter.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: react like enter when press space bar with getsting function
« Reply #3 on: February 21, 2017, 06:31:02 AM »
the function getstring with t allows you to write a text with space then I want to end the input by pressing Space bar as if I am pressing Enter.
So, which key do you press to write a space in the string... :idiot2:
Speaking English as a French Frog

Coder

  • Swamp Rat
  • Posts: 827
Re: react like enter when press space bar with getsting function
« Reply #4 on: February 21, 2017, 06:43:36 AM »
the function getstring with t allows you to write a text with space then I want to end the input by pressing Space bar as if I am pressing Enter.
So, which key do you press to write a space in the string... :idiot2:

Sorry for the confusion. My bad.

I would like to end the function just if the first input is Space and not in the middle of a string if the user intended to write a long string.

Thank you for your help.

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: react like enter when press space bar with getsting function
« Reply #5 on: February 21, 2017, 06:50:43 AM »
Perhaps :

Code: [Select]
(strcat " " (getstring "\nEnter your string like you've typed space firstly : "))
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Coder

  • Swamp Rat
  • Posts: 827
Re: react like enter when press space bar with getsting function
« Reply #6 on: February 21, 2017, 09:57:33 AM »
I am sorry for this confusion.

This should be clearer enough.

Code: [Select]
(setq str (getstring t (strcat "Name of pile :")))
If I try the above codes and hit Enter the variable 'str' would be equal to "" and this is what I trying to do if I hit Space bar I would like to finish the above codes and return the ""

Thank you.

ronjonp

  • Needs a day job
  • Posts: 7526
Re: react like enter when press space bar with getsting function
« Reply #7 on: February 21, 2017, 11:01:47 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun _fuglyinput (message / gr str)
  2.   (setq str "")
  3.   (while (and (setq gr (grread nil 2)) (/= (cadr gr) 13) (/= str " "))
  4.     (cond ((= 2 (car gr))
  5.       (setq str (cond ((= 8 (cadr gr)) (substr str 1 (1- (strlen str))))
  6.             ((strcat str (chr (cadr gr))))
  7.            )
  8.       )
  9.      )
  10.     )
  11.     (princ (strcat "\r" message str))
  12.   )
  13.   (vl-string-left-trim " " str)
  14. )
  15. (_fuglyinput "Name the pile: ")
« Last Edit: February 21, 2017, 11:48:48 AM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Coder

  • Swamp Rat
  • Posts: 827
Re: react like enter when press space bar with getsting function
« Reply #8 on: February 21, 2017, 11:10:16 AM »
Thank you ronjonp so much for your great help.

I knew that it has something to do with grread but it is above my head.  :yes:

Your function seems to work but I couldn't test it correctly because it continues printing the message to command line without a pause.

Can you please rectify it?

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: react like enter when press space bar with getsting function
« Reply #9 on: February 21, 2017, 11:12:09 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun getstring_T (msg / gr break str)
  2.   (prompt msg)
  3.   (setq str "")
  4.   (while (and (not break) (setq gr (grread 2)))
  5.     (cond
  6.       ((equal gr '(2 13)) ;_ enter
  7.        (setq break T)
  8.       )
  9.       ((equal gr '(2 32)) ;_ space
  10.        (if (= str "")
  11.          (setq break T)
  12.          (setq str (strcat str (princ " ")))
  13.        )
  14.       )
  15.       ((equal gr '(2 8)) ;_ backspace
  16.        (and (/= str "")
  17.             (setq str (substr str 1 (1- (strlen str))))
  18.             (princ (chr 8))
  19.        )
  20.       )
  21.       ((= 2 (car gr)) ;_ any other char
  22.        (setq str (strcat str (princ (chr (cadr gr)))))
  23.       )
  24.     )
  25.   )
  26.   (terpri)
  27.   str
  28. )
« Last Edit: February 21, 2017, 11:18:29 AM by gile »
Speaking English as a French Frog

ronjonp

  • Needs a day job
  • Posts: 7526
Re: react like enter when press space bar with getsting function
« Reply #10 on: February 21, 2017, 11:16:40 AM »
Thank you ronjonp so much for your great help.

I knew that it has something to do with grread but it is above my head.  :yes:

Your function seems to work but I couldn't test it correctly because it continues printing the message to command line without a pause.

Can you please rectify it?
Change the "\n" to "\r".

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Coder

  • Swamp Rat
  • Posts: 827
Re: react like enter when press space bar with getsting function
« Reply #11 on: February 21, 2017, 11:20:21 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun getstring_T (msg / gr break str)
  2.   (prompt msg)
  3.   (setq str "")
  4.   (while (and (not break) (setq gr (grread 2)))
  5.     (cond
  6.       ((equal gr '(2 13)) ;_ enter
  7.        (setq break T)
  8.       )
  9.       ((equal gr '(2 32)) ;_ space
  10.        (if (= str "")
  11.          (setq break T)
  12.          (setq str (strcat str (princ " ")))
  13.        )
  14.       )
  15.       ((equal gr '(2 8)) ;_ backspace
  16.        (and (/= str "")
  17.             (setq str (substr str 1 (1- (strlen str))))
  18.             (princ (chr 8))
  19.        )
  20.       )
  21.       ((= 2 (car gr)) ;_ any other char
  22.        (setq str (strcat str (princ (chr (cadr gr)))))
  23.       )
  24.     )
  25.   )
  26.   (terpri)
  27.   str
  28. )

Great work gile.
Thank you so much.

Coder

  • Swamp Rat
  • Posts: 827
Re: react like enter when press space bar with getsting function
« Reply #12 on: February 21, 2017, 11:22:06 AM »
Change the "\n" to "\r".

Wow that was simple. and great work ronjonp.
Thank you so much.

ronjonp

  • Needs a day job
  • Posts: 7526
Re: react like enter when press space bar with getsting function
« Reply #13 on: February 21, 2017, 11:48:09 AM »
Change the "\n" to "\r".

Wow that was simple. and great work ronjonp.
Thank you so much.
You're welcome :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: react like enter when press space bar with getsting function
« Reply #14 on: February 21, 2017, 12:20:12 PM »
gile, i think it's better to change
Code: [Select]
(princ (chr 8)) to
Code: [Select]
(princ "\010 \010")

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: react like enter when press space bar with getsting function
« Reply #15 on: February 21, 2017, 03:32:04 PM »
Nice code, Gile!
I had something similair but not as good as yours, btw heres another way to write your version:

Code - Auto/Visual Lisp: [Select]
  1. ; (Grread_Getstring_T "Type something: ")
  2. (defun Grread_Getstring_T ( msg / grr Stop str)
  3.   (prompt msg)
  4.   (setq str "")
  5.   (while (not Stop)
  6.     (if (= 2 (car (setq grr (grread))))
  7.       (or
  8.         (eval
  9.           (cdr
  10.             (assoc (cadr grr)
  11.               '(  
  12.                 (13 . (setq Stop T)) ;_ enter
  13.                 (32 . (if (= "" str) (setq Stop T) (setq str (strcat str (princ " ")))))  ;_ space
  14.                 (8 . (and (/= str "") (setq str (substr str 1 (1- (strlen str)))) (princ "\010 \010"))) ;_ backspace
  15.               )
  16.             )
  17.           )
  18.         )
  19.         (setq str (strcat str (princ (chr (cadr grr)))));_ any other char
  20.       )
  21.       (setq Stop T)
  22.     )
  23.   )
  24.   (terpri) (princ)
  25.   str
  26. ); defun Grread_Getstring_T
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

ronjonp

  • Needs a day job
  • Posts: 7526
Re: react like enter when press space bar with getsting function
« Reply #16 on: February 21, 2017, 03:48:41 PM »
Nice variation :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: react like enter when press space bar with getsting function
« Reply #17 on: February 21, 2017, 03:58:47 PM »
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: react like enter when press space bar with getsting function
« Reply #18 on: February 21, 2017, 05:35:38 PM »
Another slight variation:
Code - Auto/Visual Lisp: [Select]
  1. (defun getstringt ( m / g k s )
  2.     (setq s "") (princ m)
  3.     (while
  4.         (and (= 2 (car (setq g (grread nil 2)))) (/= 13 (setq k (cadr g)))
  5.             (cond
  6.                 (   (and (= 32 k) (= s "")) nil)
  7.                 (   (= 08 k)
  8.                     (cond ((= s "")) ((princ "\010 \010") (setq s (substr s 1 (1- (strlen s))))))
  9.                 )
  10.                 (   (setq s (strcat s (princ (chr k)))))
  11.             )
  12.         )
  13.     )
  14.     s
  15. )

(@gile, note that (grread 2) is unnecessarily tracking the cursor.)