How do you detect key presses on a Racket web application?

荒凉一梦 提交于 2019-12-12 21:02:28

问题


I've been through the documentation for web-servers and can't find anything on it.

Here's my code for a basic web application:

#lang racket

(require web-server/servlet
         web-server/servlet-env)

(define test '())

(define (start request)
  (define bindings (request-bindings request))
  (cond
    ((exists-binding? `cb1 bindings)
     (set! test '(1 2 3))
     (printf "~a" "(test) has been set to '(1 2 3)!")))
  (response/xexpr
   `(html
     (head (title "My Blog"))
     (body
      (h1 "Under construction")
      (form ,`(input ((name "cb1") (type "checkbox")) (value " Checkbox 1")) 
            (p (input ((type "submit") (value "Submit")))))))))

(serve/servlet start)

I want to be able to submit without having to press submit and instead by pressing a key such as enter. Is it possible to do this?


回答1:


The Racket servlet produces a web page (in html) that is sent to the client. On the client the web page is shown in the user's browser. When the user press a key the browser needs to handle it. The only way to get the browser to do anything special on a key press is to use write a handler in JavaScript. Note that the Racket portion of your program only runs on the server.

In short: You need to write a small piece of JavaScript and embed it in the html page.

See How to submit form on keypress? for more information on how to do this in JavaScript.



来源:https://stackoverflow.com/questions/42473515/how-do-you-detect-key-presses-on-a-racket-web-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!