gpt4 book ai didi

Anyone know why pg.K_LEFT is the number 1073741904?(有人知道为什么pg.k_left是数字1073741904吗?)

转载 作者:bug小助手 更新时间:2023-10-28 11:05:16 26 4
gpt4 key购买 nike



So this is not a problem specifically, but i thought it is a bit weird. Why is pg.K_LEFT the number 1073741904 (and any other arrow key on the keyboard is strangely long)? An example of what i mean:

所以这并不是一个特别的问题,但我觉得这有点奇怪。为什么pg.k_Left是数字1073741904(键盘上的任何其他箭头键都奇怪地长)?举个例子来说明我的意思:


import pygame
pg = pygame
pg.init()
screen = pg.display.set_mode((800,600))

run = True
while run:

screen.fill((0,0,0))
for e in pg.event.get():
if e.type == pg.QUIT:
run = False

if e.type == pg.KEYDOWN:
if e.key == 1073741904:
print("pressing arrow key left")

pg.display.flip()




Running this piece of code and pressing the left arrow key prints that you pressed the left arrow key. But why is it this long number, not something shorter? Also where can i ask these questions because i know everybody hates questions that are not a problem. I will edit the post if needed. Thanks

运行这段代码并按向左箭头键将显示您按了向左箭头键。但为什么是这么长的数字,而不是更短的数字呢?另外,我可以在哪里问这些问题,因为我知道每个人都讨厌不成问题的问题。如果需要,我会编辑帖子。谢谢


更多回答

What is wrong with 1073741904. It is just a constant. PyGame uses SDL under the hood and the codes come from there.

1073741904有什么问题?这只是一个常量。PyGame在幕后使用了SDL,代码来自那里。

That is 0x40000050 in hex, looks pretty regular

这是十六进制的0x40000050,看起来很规则

优秀答案推荐

That value in hexadecimal (convenient byte representation, having 2 hex digits corresponding to exactly 8 bits) is 4000 0050h where perhaps it seems far less "random".

该十六进制的值(方便的字节表示法,有两个十六进制数字正好对应于8位)是4000 0050h,其中它可能看起来远没有那么“随机”。


You should not in any event use literals (decimal or hex) for this. Pygame defines symbolic constants for all keys. See https://www.pygame.org/docs/ref/key.html. In this case K_LEFT.

在任何情况下,您都不应为此使用文字(十进制或十六进制)。PYGAME为所有键定义了符号常量。请参阅https://www.pygame.org/docs/ref/key.html.在本例中为K_Left。


The binary structure of these values is important since it allows modifiers to be applied by bitwise OR-ing the base key values with modifiers such that for example Ctrl + Shift + would be K_LEFT | KMOD_CTRL | KMOD_SHIFT.

这些值的二进制结构很重要,因为它允许通过将基本键值与修饰符逐位或来应用修饰符,例如CTRL+SHIFT+←将是K_LEFT|KMOD_CTRL|KMOD_SHIFT。


Without reference to all the values, I would imagine that if you look at the codes, regular character keys are all 8 bit significant digits, the 40h in the LSB (or rather bit 30 alone) is a modifier indicating a non-printing control key perhaps. Note that these values are not thew same across versions of Pygame - another reason not to use the literal value.

如果不参考所有的值,我会想象如果您查看代码,常规的字符键都是8位有效数字,LSB中的40H(或者更确切地说,单独的位30)可能是指示非打印控制键的修饰符。请注意,这些值在不同版本的PyGame中并不相同,这是不使用文字值的另一个原因。


In fact I took a look, the basic (unmodified) key codes are defined (weirdly in decimal) at https://github.com/pygame/pygame/blob/main/src_c/key.c.

事实上,我看过了,基本的(未修改的)按键代码是在https://github.com/pygame/pygame/blob/main/src_c/key.c.定义的(奇怪的十进制


更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com