且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检测Racket网络应用程序上的按键?

更新时间:2023-10-20 08:48:10

Racket servlet生成发送给客户端的网页(以html格式)。
在客户端,网页显示在用户的浏览器中。当用户按下一个键时,浏览器需要处理它。让浏览器在按键上执行任何特殊操作的唯一方法是在JavaScript中使用写入处理程序。请注意,程序的Racket部分只能在服务器上运行。

简单地说:您需要编写一小段JavaScript并将其嵌入html页面。

请参阅如何提交表单on keypress?
了解如何在JavaScript中执行此操作的更多信息。


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?

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.