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

Adds function to pygame.key module to get key code from name of key #1694

Merged
merged 8 commits into from May 5, 2020

Conversation

MyreMylar
Copy link
Contributor

Attempt to add a function to get a key code from a key name. There were some concerns in the original report #309 that this function wouldn't be SDL1 compatible but I'm not sure if that is a huge concern any more.

Also, do I need to add a:

.. versionadded:: 2.0.0 

into the docs?

It's my first first attempt at adding a C extension Function so I may have done some obvious stuff wrong!

@MightyJosip
Copy link
Contributor

Great. Can you also add
def key_code(name: str) -> int: ...
somewhere in the buildconfig/pygame-stubs/key.pyi. And yes it would be a cool addition if you write version in the docs.

@illume
Copy link
Member

illume commented May 1, 2020

I think this needs to be more clear in the docs what this is returning.

Mention something about it matching K_* event types and an example like your unit test below?

   Example:

   ::

        >>> pygame.key.key_code("return") == pygame.K_RETURN
        True
        >>> pygame.key.key_code("0") == pygame.K_0
        True
        >>> pygame.key.key_code("space") == pygame.K_SPACE
        True

   .. ## pygame.key.key_code ##

We still want to be able to compile with SDL 1 for pygame 2.

#if IS_SDLv1
/* maybe return 0? How would code that has to work on pygame 1 and 2 work? */
#else  /* IS_SDLv2 */
/*... your code goes here.*/
#endif /* IS_SDLv2 */

The SDL docs for SDL_GetKeyFromName have this note:
https://wiki.libsdl.org/SDL_Keycode
https://wiki.libsdl.org/SDL_GetKeyFromName

Returns key code, or SDLK_UNKNOWN if the name wasn't recognized; call SDL_GetError() for more information.

So your code needs to either raise an error if you get SDLK_UNKNOWN? Or should it just return 0? Maybe raising an error is better.

@MyreMylar
Copy link
Contributor Author

Added two exceptions:

  1. Where the key name is unknown it returns 0 and raises a ValueError.
  2. Where pygame is compiled with SDL 1 it should raise a NotImplementedError. Not certain how to test this one, I added a test for it but I don't know if the CI runs that setup.

And I added the example documentation suggested by @illume.

test/key_test.py Show resolved Hide resolved
src_c/key.c Outdated Show resolved Hide resolved
@MyreMylar MyreMylar changed the title Get key from name Adds function to pygame.key module get key code from name of key May 2, 2020
@MyreMylar MyreMylar changed the title Adds function to pygame.key module get key code from name of key Adds function to pygame.key module to get key code from name of key May 2, 2020
@illume illume added the key pygame.key label May 2, 2020
@MightyJosip
Copy link
Contributor

MightyJosip commented May 3, 2020

Thanks 👍, I will approve this changes now, but since we are close to the release date I will let illume to decide if we merge it in dev8 or later.

Copy link
Member

@illume illume left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 thanks

@illume illume merged commit 22015cc into pygame:master May 5, 2020
@MyreMylar MyreMylar deleted the get-key-from-name branch June 5, 2020 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
key pygame.key
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants