Пример #1
0
// key HandleFunc for the http /key endpoint. This only happens if the client
// doesn't support websockets.
func (t *tbfe) key(w http.ResponseWriter, req *http.Request) {
	log.Debug("key: %s", req)
	kc := req.FormValue("keyCode")
	var kp keys.KeyPress
	v, _ := strconv.ParseInt(kc, 10, 32)

	if req.FormValue("altKey") == "true" {
		kp.Alt = true
	}
	if req.FormValue("ctrlKey") == "true" {
		kp.Ctrl = true
	}
	if req.FormValue("metaKey") == "true" {
		kp.Super = true
	}
	if req.FormValue("shiftKey") == "true" {
		kp.Shift = true
	}

	if !kp.Shift {
		v = int64(unicode.ToLower(rune(v)))
	}
	kp.Key = keys.Key(v)
	kp.Text = string(v)
	backend.GetEditor().HandleInput(kp)
}
Пример #2
0
func (t *tbfe) WebsocketServer(ws *websocket.Conn) {
	clients = append(clients, ws)

	// Send status message
	if t.status_message != "" {
		websocket.JSON.Send(ws, map[string]string{"type": "statusMessage", "msg": t.status_message})
	}

	// Send cursor position
	websocket.JSON.Send(ws, t.GetSelectionMessage(backend.GetEditor().ActiveWindow().ActiveView()))

	// Send editor content
	var buf bytes.Buffer
	t.render(bufio.NewWriter(&buf))
	websocket.Message.Send(ws, buf.Bytes())
	buf.Reset()

	var data map[string]interface{}
	var kp keys.KeyPress
	for {
		err := websocket.JSON.Receive(ws, &data)
		if err != nil {
			log.Error(err)
			return
		}
		//log.LogDebug("Received: %s", data)

		msgType := data["type"].(string)

		if msgType == "key" {
			kp.Alt = data["altKey"].(bool)
			kp.Ctrl = data["ctrlKey"].(bool)
			kp.Super = data["metaKey"].(bool)
			kp.Shift = data["shiftKey"].(bool)

			if keyName, ok := data["key"].(string); ok {
				if utf8.RuneCountInString(keyName) == 1 { // One char
					r, _ := utf8.DecodeRuneInString(keyName)
					kp.Key = keys.Key(int64(r))
				} else {
					// TODO: automatic lookup instead of this manual lookup
					// See https://github.com/limetext/lime/pull/421/files#r19269236
					keymap := map[string]keys.Key{
						"ArrowLeft":   keys.Left,
						"ArrowUp":     keys.Up,
						"ArrowRight":  keys.Right,
						"ArrowDown":   keys.Down,
						"Left":        keys.Left,
						"Up":          keys.Up,
						"Right":       keys.Right,
						"Down":        keys.Down,
						"Enter":       keys.Enter,
						"Escape":      keys.Escape,
						"Backspace":   keys.Backspace,
						"Delete":      keys.Delete,
						"Del":         keys.Delete, // Deprecated: some old browsers still use "Del" instead of "Delete"
						"KeypadEnter": keys.KeypadEnter,
						"F1":          keys.F1,
						"F2":          keys.F2,
						"F3":          keys.F3,
						"F4":          keys.F4,
						"F5":          keys.F5,
						"F6":          keys.F6,
						"F7":          keys.F7,
						"F8":          keys.F8,
						"F9":          keys.F9,
						"F10":         keys.F10,
						"F11":         keys.F11,
						"F12":         keys.F12,
						"Insert":      keys.Insert,
						"PageUp":      keys.PageUp,
						"PageDown":    keys.PageDown,
						"Home":        keys.Home,
						"End":         keys.End,
						"Break":       keys.Break,
					}

					if key, ok := keymap[keyName]; ok {
						kp.Key = key
					} else {
						log.Debug("Unknown key: %s", keyName)
						continue
					}
				}
			} else {
				v := int64(data["keyCode"].(float64))
				if !kp.Shift {
					v = int64(unicode.ToLower(rune(v)))
				}
				kp.Key = keys.Key(v)
				kp.Text = string(v)
			}

			backend.GetEditor().HandleInput(kp)
		} else if msgType == "command" {
			command := data["name"].(string)
			//args := data["args"].([]string) //TODO: add arguments support

			ed := backend.GetEditor()
			go ed.RunCommand(command, make(backend.Args))
		} else {
			log.Info("Unhandled message type: %s", msgType)
		}
	}
}