R Shiny key input binding

前端 未结 4 2066
旧时难觅i
旧时难觅i 2020-11-27 15:25

In a Shiny application, is it possible to have a binding that listens to what key a user presses down?

I\'m not too familiar with JavaScript, but I\'m looking for s

4条回答
  •  一向
    一向 (楼主)
    2020-11-27 16:08

    I've been working on an R package {keys} to solve this problem. It's basically a wrapper around the Mousetrap javascript library. With this, observing keys is an easy task:

    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)
    )
    
    server <- function(input, output, session) {
      observeEvent(input$keys, {
        print(input$keys)
      })
    }
    
    shinyApp(ui, server)
    

    More information here: https://github.com/r4fun/keys

提交回复
热议问题