Facepunch / garrysmod-issues

Garry's Mod issue tracker
144 stars 56 forks source link

chromium javascript sees symbol keyboard keys as 0 in onkeydown and onkeyup #3543

Open SpiralP opened 6 years ago

SpiralP commented 6 years ago

On the Chromium branch, the onkeydown and onkeyup events can't see the key code of symbol keys such as ,./-=[];' as well as alt and meta. These all come up as 0 on event.keyCode.

local a = vgui.Create("DHTML") timer.Simple(10, function() a:Remove() end)
a:SetPos(0, 0)
a:SetSize(200, 200)
a:SetHTML[[
    <html><body style="background-color: white">
        <script type="text/javascript">
            document.onkeydown = function(e) { console.log("down " + e.keyCode); };
            document.onkeyup = function(e) { console.log("up " + e.keyCode); };
            document.onkeypress = function(e) { console.log("press " + e.keyCode); };
        </script>
    </body></html>
]]
a:AddFunction("console", "log", chat.AddText)
a:SetVisible(true)
a:MakePopup()

pressing and releasing shift, then the m key, then the , comma key

chromium        awesomium       chrome browser for reference
down 160        down 16         down 16         (not sure why these are different?)
up 160          up 16           up 16

down 77         down 77         down 77
press 109       press 109       press 109
up 77           up 77           up 77

down 0          down 188        down 188        <---
press 44        press 44        press 44
up 0            up 188          up 188          <---

(note keydown/keyup vs keypress having different codes is normal, one shows the physical code and the other shows the character code)

We have an ingame editor on our server, and not being able to ctrl-/ line comment is annoying.

chromium/180331 Thanks again!

meepen commented 6 years ago

keyCode is deprecated, should probably use something else

SpiralP commented 6 years ago

Alright, I changed my code to use KeyboardEvent.key, but this always shows up as undefined on Awesomium, and is still missing the same keys on Chromium(180801).

local a = vgui.Create("DHTML") timer.Simple(10, function() a:Remove() end)
a:SetPos(0, 0)
a:SetSize(200, 200)
a:SetHTML[[
    <html><body style="background-color: white">
        <script type="text/javascript">
            document.onkeydown = function(e) { console.log("down " + e.key); };
            document.onkeyup = function(e) { console.log("up " + e.key); };
            document.onkeypress = function(e) { console.log("press " + e.key); };
        </script>
    </body></html>
]]
a:AddFunction("console", "log", chat.AddText)
a:SetVisible(true)
a:MakePopup()

Here's me pressing shift, control, windows key, alt, comma, /, then the m key

down Shift
up Shift

down Control
up Control

down Unidentified -- windows meta
up Unidentified

down Unidentified -- alt
up Unidentified

down Unidentified
press ,
up Unidentified

down Unidentified
press /
up Unidentified

down m
press m
up m
BadgerCode commented 5 years ago

If the character you are trying to detect is displayable, the keypress event might work in your scenario (even though it is deprecated).

This is most annoying for the "escape" key. I have a HTML panel I need to hide when the user opens the menu/console. Otherwise, my panel displays on top of the menu, making the menu difficult to use.