Pygame won't read any uppercase keys - pygame

As an example, when I try to input (through py.event.get()) the ^ (above the 6), the event reads the shift key, but not the six. The same is true for capital letters and #, #, $, %, *, (, and ). The code below is in a while loop. It works for all the keys that don't require a shift.
for event in py.event.get():
if event.type == py.KEYDOWN:
keys = py.key.get_pressed()
if keys[py.K_CARET]:
print("^")
if event.type == py.K_CARET:
print("^")

See How to get keyboard input in pygame?
pygame.key.get_pressed() returns a list with the state of all keyboard buttons. This is not intended to get the key of a keyboard event. The key that was pressed can be obtained from the key attribute (not type) of the pygame.event.Event object. This also works for uppercase keys:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
print(event.key, event.unicode)
If this doesn't work for you, this is not a general problem, it is related to your system.

Related

Tkinter link button press and 'return' key

I've looked at various tutorials for how to link a button press and a keyboard press of 'return.' I can do them both, but only one at a time. The keyboard style is rootWindow.bind('<Return>' functionName and that linking an on-screen button is command=functionName. I just can't get them to work when used at the same time.
This is the very basic skeleton of what I'm working with
def printthis(event):
print("worked")
root = Tk()
root.bind('<Return>', printthis)
button1 = Button(root, text='Enter', command=printthis)
button1.pack()
root.mainloop()
I get this error when I run the current code, I'm just not sure what 'event' I should pass into the command=printthis section
TypeError: printthis() missing 1 required positional argument: 'event'
As a side note I am using Python 3.x
When you bind a function an event object will be passed into it whenever the binding action occurs. If you want your function to work for both on event and button press then you have two options.
First is if your function may require the event object. Then allow your function to take an optional argument for event.
def printthis(event = None):
if event is None:
# handle this case
# otherwise handle event object normally.
The second is if you don't care about the event object then the 1st is still fine (you just never use the event parameter), or you can use lambda when binding.
def printthis():
print("worked")
root.bind('<Return>', lambda e: printthis())
lambda takes e which is the event object and then calls your function without passing in e discarding it.

Pygame Key Listener for Python 3

I want to be able to control some motors that are hooked up to my raspberry pi through keyboard presses. I have code that turns the motors one direction for 5 seconds and then the other direction for 5 seconds before turning them off. I want to use the key listener function of pygame to control the motors through keyboard presses. I am using the following as a test on the keyboard press aspect.
import pygame
pygame.init()
pygame.key.set_repeat(100, 100)
while 1:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
print 'go forward'
if event.key == pygame.K_s:
print 'go backward'
if event.type == pygame.KEYUP:
print 'stop'
When I run this script, I do not get any errors, so I know it runs. However, when I press either the 'w' or 's' key, what displays is either a 'w' or an 's' as if I was just typing. All I want is to be able to execute a function by pressing a key. If there is a different/better way to do it that would be fine.
Pygame KEYDOWN events are looking for key presses in the active window.
You must create a window to tell Pygame where to read events from.
Another solution would be to hook keyboard events from the terminal so you wouldn't have to have a window initialized or in focus, using something like this: Python Key Binding or using the pyHook library to make global keyboard event hooks.
P.S - I realise you solved the problem yourself, I'm including this for completeness.

How to use letter/number combinations in pygame, event.key?

I know that in pygame, for the command:
if event.key==K_r:
That when you press the letter r on the keyboard you get a response. But I would like to increase the complexity to a letter/number combination for a jukebox. So I tried:
if event.key==K_r3:
And I pressed r then I pressed 3. But I get an error: name 'K_r3' is not defined. I'm hoping I don't have to write out a definition for each possible letter/number combination. Any suggestions?
`Don't fret Rico, there is an answer. Try the following:
if event.type == KEYDOWN:
#this says that a key was pressed down
keys = pygame.key.get_pressed()
#this is a list of all the keys that were pressed down. now, if you want to do do multiple keys, the answer is as follows:
if keys[K_3] and keys[K_r]:
(function)
I hope this helps you on your quest to pygame greatness.

How do I emulate a keypress inside a Vim function?

I'll start with code
function BigScrollUp()
let count = 20
while count > 0
"Press" CTRL-Y <-- how do I emulate this?
sleep 5m
count -= 1
endwhile
endfunction
I want to create a function to quickly scroll up and down, with animation so that I can keep track of where I am going.
You can use feedkeys(). Type :help feedkeys to read more:
feedkeys({string} [, {mode}]) *feedkeys()*
Characters in {string} are queued for processing as if they
come from a mapping or were typed by the user. They are added
to the end of the typeahead buffer, thus if a mapping is still
being executed these characters come after them.
The function does not wait for processing of keys contained in
{string}.
To include special keys into {string}, use double-quotes
and "\..." notation |expr-quote|. For example,
feedkeys("\<CR>") simulates pressing of the <Enter> key. But
feedkeys('\<CR>') pushes 5 characters.
If {mode} is absent, keys are remapped.
{mode} is a String, which can contain these character flags:
'm' Remap keys. This is default.
'n' Do not remap keys.
't' Handle keys as if typed; otherwise they are handled as
if coming from a mapping. This matters for undo,
opening folds, etc.
Return value is always 0.
call feedkeys("\<C-Y>")
Try this:
" Press CTRL-Y:
normal <Ctrl+v><Ctrl+y>
Literally type ctrl+v, followed by ctrl+y which will result in a single character shown as ^Y in your script.

Custom Events in pygame

I'm having trouble getting my custom events to fire. My regular events work fine, but I guess I'm doing something wrong. Here is the relevant code:
evt = pygame.event.Event(gui.INFOEVENT, {'time':time,'freq':freq,'db':db})
print "POSTING", evt
pygame.event.post(evt)
.... Later ....
for event in pygame.event.get():
print "GOT", event
if event.type == pygame.QUIT:
sys.exit()
dispatcher.dispatch(event)
gui.INFOEVENT = 101 by the way. The POSTING print statement fires, but the GOT one never shows my event.
Thanks!
From http://www.pygame.org/docs/ref/event.html
All events have a type identifier. This event type is in between the values of NOEVENT and NUMEVENTS. All user defined events can have the value of USEREVENT or higher. It is recommended make sure your event id's follow this system.
My thought is that your event ID is to high.
It seems to work when I changed the code from:
INFOEVENT = 101
to:
INFOEVENT = pygame.USEREVENT+x
where x is some positive integer.