Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keybindings work only with a specific locale #11758

Closed
ilihh opened this issue Jun 3, 2023 · 6 comments
Closed

Keybindings work only with a specific locale #11758

ilihh opened this issue Jun 3, 2023 · 6 comments
Labels

Comments

@ilihh
Copy link

ilihh commented Jun 3, 2023

Important Information

mpv version: 0.35.0-407-g959ef843
Windows 10 Pro 22H2 19045.2965
Source of the mpv binary: https://sourceforge.net/projects/mpv-player-windows/?path=/64bit

Reproduction steps

  1. Install any non-latin keyboard locale: Japanese, Korean, Arabic, etc.
  2. input.conf: add keybinding "p cycle pause"
  3. change the keyboard locale to the installed non-latin one
  4. run mpv
  5. press "p" and nothing happens
  6. run mpv with "--input-test" and non-latin locale
  7. press "p" and will be displayed "Key [non-latin character on p key] is bound to: (nothing)"

Expected behavior

Keybindings should work with any locale.
Hotkey should be bound to the physical key on the keyboard, not to the character generated by pressing the key.

Actual behavior

Keybindings work only when using the same locale as the specified characters in the input.conf

Log file

output.txt

@ilihh ilihh added the os:win label Jun 3, 2023
@CounterPillow
Copy link
Contributor

Hotkey should be bound to the physical key on the keyboard, not to the character generated by pressing the key.

disagree, having y be z for me would be insanely confusing

@N-R-K
Copy link
Contributor

N-R-K commented Jun 3, 2023

Hotkey should be bound to the physical key on the keyboard, not to the character generated by pressing the key.

I'm not aware of any application that works like this (at least not by default). Closest thing I can recall is the dwm-keycode patch that modifies dwm to use X11 keycodes instead of keysyms.

@avih
Copy link
Member

avih commented Jun 3, 2023

Hotkey should be bound to the physical key on the keyboard, not to the character generated by pressing the key.

Well, mpv is designed to invoke the command which is bound to the text which the key produces. If you change locale and as a result a key produces different text, then it will no longer invoke the command which is bound to the previous text it produced.

That is by design, and is unlikely to change. See https://mpv.io/manual/master/#key-names

Note that some names are symbolic and should work in any locale, like LEFT or TAB etc.

So you have few options:

  1. Ensure that the locale is English while invoking mpv key bindings.
  2. Bind your own in any locale you want, to replicate the default bindings you care about. The default bindings are defined here https://github.com/mpv-player/mpv/blob/master/etc/input.conf
  3. If you feel adventurous, write an mpv lua/js script which translates key presses in some locale to English. You'd need to bind the UNMAPPED key symbol (which captures all unbound keys/combos), interpret the key name as text and possibly modifiers, map the text to English, re-compose a key string in English, and use the keypress command to generate the mapped key.

@ilihh
Copy link
Author

ilihh commented Jun 3, 2023

@N-R-K

I'm not aware of any application that works like this (at least not by default). Closest thing I can recall is the dwm-keycode patch that modifies dwm to use X11 keycodes instead of keysyms.

Cannot say about *nix soft, but in Windows hotkeys are locale independent.
For example, both Ctrl+p and Ctrl+せ will open a print dialog in the browser, because it is the same physical keys.

@avih
Ok, then the issue can be closed.

@ilihh ilihh closed this as not planned Won't fix, can't repro, duplicate, stale Jun 3, 2023
@garoto
Copy link
Contributor

garoto commented Jun 3, 2023

Old and busy related issue (most interesting discussion happens around the 2016 time frame imo): #351

@avih
Copy link
Member

avih commented Jun 6, 2023

3. If you feel adventurous, write an mpv lua/js script which translates key presses in some locale to English. You'd need to bind the UNMAPPED key symbol (which captures all unbound keys/combos), interpret the key name as text and possibly modifiers, map the text to English, re-compose a key string in English, and use the keypress command to generate the mapped key.

For completeness and future reference, here's such a lua script which can map a key (maybe with some modifiers) to another key (with the same modifiers).

It includes an example mapping of the keypad (KP_...) to the normal editing keys, but one can replace/add any other mappings, e.g. from some locale to English.

Save it as key-remapper.lua in your ~~/scripts dir:

-- mpv key mapper by avih. see https://github.com/mpv-player/mpv/issues/11758

-- known issue: auto-repeat might not work for mapped keys at the mpv window,
-- but may work at the terminal. --native-keyrepeat might help. see note below.

-- add (or remove) as many mapping as needed at keymap below, but note
-- that this only works for unmapped key events, i.e. when mpv decides
-- that some key (maybe with modifiers) is not bound, then this map kicks in

-- keymap is a list of: ["<SOURCE-KEY>"] = "<TARGET-KEY>"
-- e.g. ["KP1"] = "END" so that all KP1 key events (with or without
--      modifiers) will translate to END key events with the same modifiers.
local keymap = {
    -- example: map keypad keys to normal editing keys
    ["KP1"] = "END",
    ["KP2"] = "DOWN",
    ["KP3"] = "PGDWN",
    ["KP4"] = "LEFT",
    -- KP5 doesn't seem to have an editing function?
    ["KP6"] = "RIGHT",
    ["KP7"] = "HOME",
    ["KP8"] = "UP",
    ["KP9"] = "PGUP",

    -- maybe add a map of key text in some locale to english keys
}


local msg = require("mp.msg")

-- splits an mpv key name to modifiers (possibly empty string) and key.
-- the original key name is always a concatenation of these two parts.
local function key_split(keyname)
    if keyname:find("%+.+") then
        return keyname:match("(.*%+)(.+)")
    end
    return "", keyname
end

local key_ev_to_cmd = {
    down = "keydown",
    up = "keyup",
    press = "keypress",
    ["repeat"] = "keypress",
}

mp.add_key_binding("UNMAPPED", function(t)
    if t.key_name:find("MOUSE", 1, true) then
        return  -- we don't map mouse commands
    end

    local cmd = key_ev_to_cmd[t.event]
    local modifiers, key = key_split(t.key_name)
    local newkey = keymap[key]

    if not cmd then
        msg.error("No key event map for", t.event)
    elseif newkey == key then
        msg.error("error: key '".. key .. "' is mapped to itself")
    elseif not newkey then
        if cmd ~= "keyup" then  -- message only on keydown/press
            msg.warn("No binding or map found for key '" .. t.key_name .. "'")
        end
    else
        msg.verbose(cmd, t.key_name, "->", modifiers .. newkey)
        mp.commandv(cmd, modifiers .. newkey)
    end
end, {complex=true})


-- note about autorepeat
--
-- we map repeat, but it doesn't always work where it matters.
-- if we don't generate any key event in response to a "repeat" event then
-- it will keep repeating, but if we generate a (mapped) key event for some
-- other key, then mpv automatically first cancels any currently-pressed key,
-- hence it will abort the autorepeat of the original key (and we won't get
-- a keyup event either).
--
-- one could think that using --native-keyrepeat to utilize the OS
-- autorepeat functionality could work around this, but at least
-- on Windows it doesn't seem to work, i.e. autorepeat still doesn't work
-- for mapped keys. it might work on other platforms though, so try it out.
--
-- However, while the above is true for the mpv window (where the VO and
-- mpv itself handle low-level key events - down, up, repeat), key events
-- at the terminal are handled by the terminal. In this case key-repeat
-- will send repeated key presses to mpv, and so autorepeat may work also
-- for mapped keys if pressed at the terminal window.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants