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

Fix documentation on how to get the keycode string from a physical_keycode #82092

Merged

Conversation

jitspoe
Copy link
Contributor

@jitspoe jitspoe commented Sep 22, 2023

Bugsquad edit: Linked issue.

@jitspoe jitspoe requested a review from a team as a code owner September 22, 2023 05:01
@dalexeev dalexeev added this to the 4.2 milestone Sep 22, 2023
@akien-mga akien-mga changed the title Fixed documentation on how to get the keycode string from a physical_keycode. Fixed documentation on how to get the keycode string from a physical_keycode. Nov 8, 2023
@akien-mga akien-mga changed the title Fixed documentation on how to get the keycode string from a physical_keycode. Fix documentation on how to get the keycode string from a physical_keycode Nov 8, 2023
…eycode`

Fixes godotengine#82091.

Co-authored-by: Yuri Sizov <11782833+YuriSizov@users.noreply.github.com>
Co-authored-by: Rémi Verschelde <rverschelde@gmail.com>
@akien-mga akien-mga force-pushed the master.pr_physical_keycode_doc_fix branch from 8a993a2 to 1e01fcd Compare November 9, 2023 12:04
@akien-mga
Copy link
Member

akien-mga commented Nov 9, 2023

Added @YuriSizov's suggestion, and further reworked it to be valid code, with a C# example. It's now a full _input function example as C# needs @event to be properly defined in that context.

I tested both GDScript and C# snippets successfully.

@akien-mga akien-mga requested review from bruvzg and YuriSizov November 9, 2023 12:05
@akien-mga akien-mga merged commit 95a42ae into godotengine:master Nov 9, 2023
15 checks passed
@akien-mga
Copy link
Member

Thanks!

@stephanbogner
Copy link

Are we sure the code is working correctly?
(If not I'd open an issue for get_keycode_string)

I am on German QWERTZ keyboard on a Mac where the following happens:

  • Press ZZ
  • Press QQ
  • Press +Plus ✅ (even though I'd prefer +)
  • Press -Minus ✅ (even though I'd prefer -)
  • Press ÜBracketLeft
  • Press ÄApostrophe
  • Press ÖSemicolon

It's really weird, because it seems the Umlauts (ÄÖÜ) fall back to an US keyboard layout but everything else works as expected.

Also it doesn't put it out in the user's language but that's probably a different issue/topic (which would be important when showing it to the actual user).

@AThousandShips
Copy link
Member

AThousandShips commented Sep 6, 2024

If you're using physical_keycode that's how it works, see the documentaiton:

Represents the physical location of a key on the 101/102-key US QWERTY keyboard, which corresponds to one of the Key constants.

The instruction is for how to convert that physical keycode into a human readable string of that keycode, not of the layout specific keycode, for that use keycode

However if it does return Z for the Y key on the QWERTY keyboard that is a bug or an OS side issue, that should be investigated, but the umlauts (assuming they are the keys near the enter key, next to P) are correct

It does however seem like you're using keycode not physical_keycode as you are getting Z mapped to Z, the event should have keycode=Z, physical_keycode=Y a QWERTZ layout, and use the bracket keys for the umlauts AFAIK as they don't have keycodes (you need to use the unicode instead for those)

@stephanbogner
Copy link

stephanbogner commented Sep 6, 2024

@AThousandShips

Ah ok, I understood it as physical refers to the physical keyboard I have in front of me (my bad) ... but there still seems to be a bug somewhere (gosh, how much I hate cross-platform issues).

I used the code from the docs:

func _input(event):
	if event is InputEventKey:
		var keycode = DisplayServer.keyboard_get_keycode_from_physical(event.physical_keycode)
		print(OS.get_keycode_string(keycode))

It seems to mix layouts in a weird manner:
As you can see from the video when typing the keyboard works as normal (not a different layout applied), but the output from the log is like a mix of the US and the German QWERTY layout, e.g.:

Ü = BracketLeft (US)
# = NumberSign (German)
- = Minus (German)
ß = Minus (US)
´ = Equal (US)

Tested on a MacBook Pro with 14.6.1 (Sonoma) and Godot v4.2.2

IMG_0383.mp4

@AThousandShips
Copy link
Member

Please open an issue to investigate this, could be many different issues, also please provide a clear log of the results (I'd suggest making sure it prints both the keycode and the physical keycode, both in human readable forms, to evaluate), and an MRP

@stephanbogner
Copy link

@AThousandShips

Will do!

Would the following be a good setup to test it in your opinion:

func _input(event):
	if event is InputEventKey and event.is_released():
		print("")
		print("Keycode: ", event.keycode, " -> ", OS.get_keycode_string(event.keycode))
		print("Physical keycode: ", event.physical_keycode, " -> ", OS.get_keycode_string(event.physical_keycode))
		var keycode_from_physical = DisplayServer.keyboard_get_keycode_from_physical(event.physical_keycode)
		print("Keycode (from physical): ", keycode_from_physical, " -> ", OS.get_keycode_string(keycode_from_physical))

Which might already have the conclusion:
✅ Pressing - prints:

Keycode: 45 -> Minus
Physical keycode: 47 -> Slash
Keycode (from physical): 45 -> Minus

❌ Pressing ß prints:

Keycode: 45 -> Minus
Physical keycode: 45 -> Minus
Keycode (from physical): 45 -> Minus

❌ Pressing Ä prints:

Keycode: 39 -> Apostrophe
Physical keycode: 39 -> Apostrophe
Keycode (from physical): 39 -> Apostrophe

@AThousandShips
Copy link
Member

That doesn't look incorrect, remember that there aren't keycodes for symbols not on the US keyboard, they are mapped to the one most relevant, so ß should be correct, as it is where - is in the US keyboard, same for Ä, which is where ' is on a US keyboard

Compare with the correct layout here

@bruvzg
Copy link
Member

bruvzg commented Sep 6, 2024

Pressing ß prints:

Both keycode and physical_keycode are limited to the set of codes on the 101/102-key US QWERTY keyboard, and there's no ß on it. If you want what's to get what's printed on the key, use key_label instead.

@AThousandShips
Copy link
Member

Forgot about key_label

For more info see here

@stephanbogner
Copy link

Ahhhh! Thanks @bruvzg ❤️ That does the trick! I didn't expect you have to plug something else into OS.get_keycode_string

I will post an example here for the people that also get confused:

Press Ä on a QWERTZ keyboard will result in:

  • event.key_label = 196
  • OS.get_keycode_string(event.key_label) = Ä
  • OS.get_keycode_string(event.keycode) = Apostrophe
  • OS.get_keycode_string(event.physical_keycode) = Apostrophe

@bruvzg
Copy link
Member

bruvzg commented Sep 6, 2024

  • physical_keycode → position of the key, e.g., WASD for movement.
  • keycode → basic Latin shortcut available on most layouts (but might be in different places), e.g., Ctrl+C/Ctrl+V for copy/paste.
  • key_label → what's printed on the key.
  • unicode → text input (with modifiers, and not necessarily associated with a real key press, since can be emitted by IME/virtual keyboard, etc.).

More examples (I should add this image and some info to the docs):
KeyboardTranslation

@Zireael07
Copy link
Contributor

More examples (I should add this image and some info to the docs):

Yes PLEASE!

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

Successfully merging this pull request may close these issues.

Documentation on physical_keycode is incorrect
9 participants