Register repeated keyboard presses in shiny

Viewed 555

I'm trying to build an application where keyboard presses are captured, however I've run into the issue that if the same keyboard presses are used successively then shiny doesn't seem to register the input. Wondering if there is a way around this.

E.g., here's what I mean.

library(shiny)
runApp( list(ui = bootstrapPage(
    verbatimTextOutput("results"),
    tags$script('
    $(document).on("keydown", function (e) {
       Shiny.onInputChange("down", e.which);
    });'),
    tags$script('
    $(document).on("keyup", function (e) {
                Shiny.onInputChange("up", e.which);
                });')
)
, server = function(input, output, session) {

    output$results = renderPrint({
        print(rnorm(1))
        c(input$down, input$up)
    })
}
))

Typing/releasing different characters on the keyboard generates new input, hence the random number generator is called when these events occur. But typing, say, 'g' 'g' 'g' only registers the first up-down keystrokes and ignores the rest.

1 Answers
Related