Author Topic: ReadKey Method  (Read 3300 times)

0 Members and 1 Guest are viewing this topic.

Grrr1337

  • Swamp Rat
  • Posts: 812
ReadKey Method
« on: January 20, 2018, 05:22:18 PM »
Hi guys,
Is there a way (aside from grread) to track user's input?
I'm refering to something like this (only if it was possible to call it with LISP).

 :rolleyes2:
(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

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ReadKey Method
« Reply #1 on: January 20, 2018, 07:05:36 PM »
May have read your want wrong (I’m on my phone and blind after hot yoga) but perhaps this post from yesteryear.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: ReadKey Method
« Reply #2 on: January 20, 2018, 08:00:06 PM »
No, Michael - my question is more related to the scripting objects - if theres a method that mimics the grread function.
So maybe it could track F1-F12 keys or track while a dialog is displayed (DCL) or maybe even modeless-ly (if theres such method - will explore).
or ctrl+key/shift+key/ctrl+shift+key, just the reverse of the 'SendKeys' method. :)
(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

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: ReadKey Method
« Reply #3 on: January 21, 2018, 05:49:46 AM »
Maybe acet-sys-keystate?

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: ReadKey Method
« Reply #4 on: January 21, 2018, 07:39:45 AM »
Maybe acet-sys-keystate?

Thanks Roy, just now performed this test (and was disappointed, as I expected to track some keys outside of grread's range) :

Code: [Select]
(defun C:test ( / s tmp )
  (princ "\nPress [ENTER] to exit: ")
  (while (not s)
    (grread) ; required - duh
    (cond ; Virtual-Key Codes [HEX] :  https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx
      ( (not (member (setq tmp (acet-sys-keystate 4)) '(0 1))) ; OK ; you'll have to spam it quickly to display the alert message, I think this key is outside of (grread) range
        (alert (strcat "[Middle Mouse Button] pressed!" "\n" (vl-prin1-to-string tmp)))
      )
      ;|
      ( (not (member (setq tmp (acet-sys-keystate ?)) '(0 1))) ; NOT ; [UNKNOWN ASCII] this key is out of (grread) range and doesn't work for (acet-sys-keystate)
        (alert (strcat "[F2] button pressed!" "\n" (vl-prin1-to-string tmp)))
      )
      ( (not (member (setq tmp (acet-sys-keystate ?)) '(0 1))) ; NOT ; [UNKNOWN ASCII] this key is out of (grread) range and doesn't work for (acet-sys-keystate)
        (alert (strcat "[UP-arrow] button pressed!" "\n" (vl-prin1-to-string tmp)))
      )
      |;
      ( (not (member (setq tmp (acet-sys-keystate 49)) '(0 1))) ; OK ; this key is within (grread) range and works for (acet-sys-keystate)
        (alert (strcat "[1] button pressed!" "\n" (vl-prin1-to-string tmp)))
      )
      ( (not (member (setq tmp (acet-sys-keystate 9)) '(0 1))) ; OK ; this key is within (grread) range and works for (acet-sys-keystate)
        (alert (strcat "[TAB] button pressed!" "\n" (vl-prin1-to-string tmp)))
      )
      ( (not (member (setq tmp (acet-sys-keystate 13)) '(0 1))) ; OK ; this key is within (grread) range and works for (acet-sys-keystate)
        (alert (strcat "[ENTER] button pressed!" "\n" (vl-prin1-to-string tmp)))
        (setq s t) (alert "Exit!")
      )
    ); cond
  ); while
  (princ)
); defun C:test

The above code still uses grread, but doesn't acquire whats returned from it (so pure acet-sys-keystate testing).
Couldn't figure out anything else.

But yeah, my thread is about knowing the alternatives for grread.
« Last Edit: January 21, 2018, 05:23:02 PM by Grrr1337 »
(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

hmspe

  • Bull Frog
  • Posts: 362
Re: ReadKey Method
« Reply #5 on: January 21, 2018, 09:34:59 AM »
Are you using Autocad or Bricscad?  Autodesk removed the cursor control keys (arrows, PgUp, etc.) from GRREAD 10 years ago or so.  I think the issue was that the older implementation did not return unique codes for all of the cursor control keys.  In recent versions of Bricscad the cursor control keys have been added back.  To prevent conflicting codes the cursor keys add 128 to the code returned.   From the release notes for V17.2.06:

BRX LISP .NET VBA
"grread" function has been extended by a control bit flag 7 (= 128). This bit flag allows to retrieve extra keys which are otherwise not tracked: cursor keys, PgUp, PgDown, Home, End, Insert, Delete. Example : (grread nil (+ 2 128))


This is for the cursor controls keys only.  The function keys above F2 do return codes with GRREAD.  I think that F1 and F2 are omitted because they are dedicated to Help and showing the prompt history. 

I use the following in an on-screen text editor to handle modified cursor keys:
Code: [Select]
  (defun get-key (/ key loop result return temp)
    (setq key (cadr (grread nil 130)))
    (cond (  (vle-member key '(3 22 24))                    ; C, V, X keys
               (if (acet-sys-control-down)                  ; test for ^C, ^V, ^X
                 (setq key (+ key 400))                     ; create a unique code
               )
          )
          (  (vle-member key '(291 292 293 294 295 296))    ; End, Home, Left, Up, Right, Down keys
               (if (acet-sys-shift-down)                    ; shift down moves and selects
                 (setq key (+ key 200))                     ; create a unique code
               )
          )
    )
    key
  )

If you're running Autocad I'd recommend looking at AutoHotKey.  Before Bricscad 'fixed' GRREAD I used AutoHotKey to add an escape code to cursor keystrokes.  Here is partial code:
Code: [Select]
#NoEnv
#Persistent
#SingleInstance force
#UseHook on
Process, priority, , high
SetTitleMatchMode 1
SendMode Input

#ifWinActive BricsCAD
{
  $Left::
    if (GetKeys() = "t")
      {
        send @L
      }
    else
      {
        send {left}
      }
  Return

  $+Left::
    if (GetKeys() = "t")
      {
        send @l
      }
    else
      {
        send {left}
      }
  Return

}

GetKeys()
{
  RegRead, keys, HKEY_CURRENT_USER, Environment, KeysOn
  Return keys
}
   

The KeysOn registry key was set to 1 when the editor's key reading loop was active and to 0 otherwise.
"Science is the belief in the ignorance of experts." - Richard Feynman

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: ReadKey Method
« Reply #6 on: January 21, 2018, 12:22:14 PM »
Hi hmspe,
Thanks for the information - I'm using AutoCAD 2017 BTW.
Still I find your first example interesting, since ACAD supports the functions acet-sys-shift-down and acet-sys-control-down, so that won't limit the ctrl+key/shift+key/ctrl+shift+key inputs.
In ACAD grread can track the ctrl key, if ctrl+key is pressed but it always returns (11 -1), so that means that its possible to determine only if ctrl was pressed (no combinations).
AutoHotKey sounds like an option.
(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

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: ReadKey Method
« Reply #7 on: January 21, 2018, 02:55:15 PM »
@Grrr1337:
The virtual key codes are expressed as hexadecimal numbers on the WWW page you mention in your code.
So the decimal key code for the '1' key is 49 and not 31. The values for 'F2' and 'Up arrow' are also wrong.
« Last Edit: January 21, 2018, 03:00:30 PM by roy_043 »

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: ReadKey Method
« Reply #8 on: January 21, 2018, 05:31:17 PM »
@Roy - I've corrected the test code in reply #4, thanks.  :oops:
Reading from here (izmirl's message) it looks like the ascii codes for these keys are out of range for lisp, since they have "an array of two rather than one".
(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

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ReadKey Method
« Reply #9 on: January 21, 2018, 10:17:42 PM »
Just to add another tangent — opendcl sports keydown / keyup events.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst