r4fun / keys

:keyboard: Keyboard Shortcuts for 'shiny'
https://r4fun.github.io/keys/
Other
45 stars 6 forks source link

Unable to use keys inside text input #11

Closed joe-chelladurai closed 3 years ago

joe-chelladurai commented 3 years ago

I've been trying to use shortcut keys while the cursor is inside the text input, but it doesn't work. It does work once I'm outside the textbox. Is there a way to fix this issue?

I found this to be a helpful resource to figure out a solution. I wish I knew more javascript to contribute. https://github.com/ccampbell/mousetrap/tree/master/plugins/global-bind

tylerlittlefield commented 3 years ago

Hey @meshach-joe thanks for providing the link to the plugin. I've made a branch that might be what you're looking for. I'll need to test it out a bit more before I merge it to master and submit to CRAN. For the time being, you can give it a spin with:

# install the branch with global bind plugin
# devtools::install_github("r4fun/keys", ref = "global-bind")

library(shiny)
library(keys)

hotkeys <- c(
  "1", 
  "command+shift+k", 
  "up up down down left right left right b a enter"
)

ui <- fluidPage(
  useKeys(),
  keysInput("keys", hotkeys, global = TRUE),
  textInput("txt", "txt input")
)

server <- function(input, output, session) {
  observeEvent(input$keys, {
    print(input$keys)
  })
}

shinyApp(ui, server)

Note that setting global = TRUE is only available in the keysInput function. So I haven't written anything to allow global key bindings with something like addKeys. Still thinking about how it should work.

joe-chelladurai commented 3 years ago

Awesome! This works very well and thanks for the very quick response.

This may be a related issue, so when I tried it with shinyAce::aceEditor, it didn't work:

library(shiny)
library(keys)

hotkeys <- c(
  "1", 
  "ctrl+shift+k", 
  "up up down down left right left right b a enter"
)

ui <- fluidPage(
  useKeys(),
  keysInput("keys", hotkeys, global = TRUE),
  textInput("txt1", "txt input"),
  aceEditor("txt", "txt input",
            height = "50px")
)

server <- function(input, output, session) {
  observeEvent(input$keys, {
    print(input$keys)
  })
}

shinyApp(ui, server)

https://github.com/trestletech/shinyAce

tylerlittlefield commented 3 years ago

Happy to hear the textInput is working! The shinyAce example you provided is also working on my end. Could you clarify what the issue is?

joe-chelladurai commented 3 years ago

I just found that it was an error on my end. So sorry! It works with shinyAce

Thanks for this wonderful package!

tylerlittlefield commented 3 years ago

No worries! I'll keep this issue open until I merge the global-bind branch to master.

vgherard commented 3 years ago

Hi! I was wondering whether you have any update on the global-bind branch. I'm depending on it for a personal package (works like charm, thanks) and was considering submitting to CRAN at some time.

Thanks, Valerio.

tylerlittlefield commented 3 years ago

@vgherard I have admittedly forgot to merge this to master. Let me re-review my work, merge to master and get this on CRAN. I'll update this thread on any status updates. Neat package by the way!

tylerlittlefield commented 3 years ago

Okay, this feature is on its way to CRAN in keys v0.1.1!

vgherard commented 3 years ago

Great, thank you so much!

Okay, this feature is on its way to CRAN in keys v0.1.1!