Single-letter shortcuts not work in graphics mode

I have some shortcuts that work as expected, shown in the image below:


However, I would prefer to have single-letter shortcuts, like the built-in ones such as + for zooming in. I checked the graphics-kbd.scm file and found that the built-in shortcuts are defined within the (:mode in-active-graphics?) mode. However, my custom single-letter shortcuts don’t work in either the in-graphics or in-active-graphics mode.

I suspect that the keypresses are not sent to the part of the program that processes them.

A hint that this is the case is that if you do

(kbd-map
  (:mode in-text?)
  ("c c" (graphics-toggle-auto-crop)))

and you press c c in a text field within a drawing, the drawing gets cropped/uncropped.

Perhaps someone else knows more.

By the way, placing the code as text in your post, instead than as an image, allows the others to copy it.

Did you put the definition in my-init-texmacs.scm? If yes, did you put it inside a (delayed (lazy-keyboard-force) ..... )? There already seems to be a graphics shortcut bound to c, so you need to make sure your code runs after that definition.

Yes, I did that. The tricky problem is the shortcuts with modifiers works, e.g. M-c, but c does not work. I don’t think c is already taken. I tried others, they don’t work neither. However, the shortcuts such as 1, 2 etc. in the system file works.

In fact, I can’t override those single-letter system shortcuts. If I put

(kbd-map
    (:mode in-active-graphics?)
    ("1" (graphics-set-color "red") 
         (graphics-set-opacity "100%"))
  )

in the init file, it does not work, and it blocks other shortcuts after these lines.

If I use in-graphics?, then it will not block the remaining code, but "1" still has no effect. I found the following definition in graphics-kbd.scm

(define (in-active-graphics?)
  (and (in-graphics?) (== (get-env "preamble") "false")))

So, it seems that graphics shortcuts are intercepted, by the overloaded function keyboard-press in graphics-kbd.scm (I don’t know why). There, only keys that are in graphics-keys are passed through.

Redefining “c” worked for me because the “c” shortcut was already defined in my version, but it only appeared recently and may not yet be in the version you are using.

Then I tried defining new graphics shortcuts. This seems to work in the latest git (if you also redefine graphics-keys to add the new key). In v2.1.2 this seems to crash TeXmacs, though.

A problem here is that you give two commands to the shortcut (one for color, one for opacity). Try bundling them together in one command as (begin (graphics-set-color "red") (graphics-set-opacity "100%")).

In fact, the shortcut works if 1 is replaced by, e.g. M-1, even without begin. It seems multiple commands are automatically concatenated. Other working examples are

("M-P" 
           (graphics-set-mode '(edit cline)) 
           (graphics-set-line-width "1ln")
           (graphics-set-color "black") 
           (graphics-set-fill-color "#4EABE8") 
           (graphics-set-opacity "50%"))

BTW, graphics-set-grid-aspect in the following code has no effect, but the interactive version works.

("M-g" 
           (make-graphics)
           (graphics-set-origin "0.1gw" "0.1gh")
           (graphics-toggle-grid)
           ;(graphics-interactive-set-grid-nsubds #t)
           (graphics-set-grid-aspect 'detailed 20 #f)
           (graphics-set-unit "2cm")
)
1 Like

Perhaps you need to experiment with the delay. These work for me (both v2.1.2 and latest git version):

(define (in-active-graphics?)
  (and (in-graphics?) (== (get-env "preamble") "false")))

(delayed (:pause 3000)
  (kbd-map
    (:mode in-active-graphics?)
    ("1" (graphics-set-color "red")
         (graphics-set-opacity "100%"))
    ("A-c" (graphics-set-color "red")
         (graphics-set-opacity "100%"))
  )
)