Python method for reading keypress?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







41















I'm new to Python, and I just made a game and a menu in Python.
Question is, that using (raw_)input() requires me to press enter after every keypress, I'd like to make it so that pressing down-arrow will instantly select the next menu item, or move down in the game. At the moment, it requires me to like type "down" and then hit enter. I also did quite a lot of research, but I would prefer not to download huge modules (e.g. pygame) just to achieve a single keyDown() method. So are there any easier ways, which I just couldn't find?



Edit:
Just found out that msvcrt.getch() would do the trick. It's not keyDown(), but it works. However, I'm not sure how to use it either, it seems quite weird, any help here? This is what I got at the moment:



from msvcrt import getch
while True:
key = getch()
print(key)


However, it keeps giving me all these nonsense bytes, for example, down-arrow is this:



b'xe0'
b'P'


And I have no idea how to use them, I've tried to compare with chr() and even use ord() but can't really do any comparisons. What I'm trying to do is basically this:



from msvcrt import getch
while True:
key = getch()
if key == escape:
break
elif key == downarrow:
movedown()
elif key == 'a':
...


And so on... Any help?










share|improve this question

























  • Not a duplicate of that. This is about keydown events, not single character input.

    – Deestan
    Aug 29 '12 at 10:48






  • 7





    I can haz cross-platform solution?? msvcrt is not available on mac/linux distributions of Python

    – cat
    Jan 11 '16 at 13:54




















41















I'm new to Python, and I just made a game and a menu in Python.
Question is, that using (raw_)input() requires me to press enter after every keypress, I'd like to make it so that pressing down-arrow will instantly select the next menu item, or move down in the game. At the moment, it requires me to like type "down" and then hit enter. I also did quite a lot of research, but I would prefer not to download huge modules (e.g. pygame) just to achieve a single keyDown() method. So are there any easier ways, which I just couldn't find?



Edit:
Just found out that msvcrt.getch() would do the trick. It's not keyDown(), but it works. However, I'm not sure how to use it either, it seems quite weird, any help here? This is what I got at the moment:



from msvcrt import getch
while True:
key = getch()
print(key)


However, it keeps giving me all these nonsense bytes, for example, down-arrow is this:



b'xe0'
b'P'


And I have no idea how to use them, I've tried to compare with chr() and even use ord() but can't really do any comparisons. What I'm trying to do is basically this:



from msvcrt import getch
while True:
key = getch()
if key == escape:
break
elif key == downarrow:
movedown()
elif key == 'a':
...


And so on... Any help?










share|improve this question

























  • Not a duplicate of that. This is about keydown events, not single character input.

    – Deestan
    Aug 29 '12 at 10:48






  • 7





    I can haz cross-platform solution?? msvcrt is not available on mac/linux distributions of Python

    – cat
    Jan 11 '16 at 13:54
















41












41








41


13






I'm new to Python, and I just made a game and a menu in Python.
Question is, that using (raw_)input() requires me to press enter after every keypress, I'd like to make it so that pressing down-arrow will instantly select the next menu item, or move down in the game. At the moment, it requires me to like type "down" and then hit enter. I also did quite a lot of research, but I would prefer not to download huge modules (e.g. pygame) just to achieve a single keyDown() method. So are there any easier ways, which I just couldn't find?



Edit:
Just found out that msvcrt.getch() would do the trick. It's not keyDown(), but it works. However, I'm not sure how to use it either, it seems quite weird, any help here? This is what I got at the moment:



from msvcrt import getch
while True:
key = getch()
print(key)


However, it keeps giving me all these nonsense bytes, for example, down-arrow is this:



b'xe0'
b'P'


And I have no idea how to use them, I've tried to compare with chr() and even use ord() but can't really do any comparisons. What I'm trying to do is basically this:



from msvcrt import getch
while True:
key = getch()
if key == escape:
break
elif key == downarrow:
movedown()
elif key == 'a':
...


And so on... Any help?










share|improve this question
















I'm new to Python, and I just made a game and a menu in Python.
Question is, that using (raw_)input() requires me to press enter after every keypress, I'd like to make it so that pressing down-arrow will instantly select the next menu item, or move down in the game. At the moment, it requires me to like type "down" and then hit enter. I also did quite a lot of research, but I would prefer not to download huge modules (e.g. pygame) just to achieve a single keyDown() method. So are there any easier ways, which I just couldn't find?



Edit:
Just found out that msvcrt.getch() would do the trick. It's not keyDown(), but it works. However, I'm not sure how to use it either, it seems quite weird, any help here? This is what I got at the moment:



from msvcrt import getch
while True:
key = getch()
print(key)


However, it keeps giving me all these nonsense bytes, for example, down-arrow is this:



b'xe0'
b'P'


And I have no idea how to use them, I've tried to compare with chr() and even use ord() but can't really do any comparisons. What I'm trying to do is basically this:



from msvcrt import getch
while True:
key = getch()
if key == escape:
break
elif key == downarrow:
movedown()
elif key == 'a':
...


And so on... Any help?







python windows key keydown getch






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '17 at 0:02









kres0345

567




567










asked Aug 29 '12 at 10:33







user1632861




















  • Not a duplicate of that. This is about keydown events, not single character input.

    – Deestan
    Aug 29 '12 at 10:48






  • 7





    I can haz cross-platform solution?? msvcrt is not available on mac/linux distributions of Python

    – cat
    Jan 11 '16 at 13:54





















  • Not a duplicate of that. This is about keydown events, not single character input.

    – Deestan
    Aug 29 '12 at 10:48






  • 7





    I can haz cross-platform solution?? msvcrt is not available on mac/linux distributions of Python

    – cat
    Jan 11 '16 at 13:54



















Not a duplicate of that. This is about keydown events, not single character input.

– Deestan
Aug 29 '12 at 10:48





Not a duplicate of that. This is about keydown events, not single character input.

– Deestan
Aug 29 '12 at 10:48




7




7





I can haz cross-platform solution?? msvcrt is not available on mac/linux distributions of Python

– cat
Jan 11 '16 at 13:54







I can haz cross-platform solution?? msvcrt is not available on mac/linux distributions of Python

– cat
Jan 11 '16 at 13:54














3 Answers
3






active

oldest

votes


















56














Figured it out by testing all the stuff by myself.
Couldn't find any topics about it tho, so I'll just leave the solution here. This might not be the only or even the best solution, but it works for my purposes (within getch's limits) and is better than nothing.



Note: proper keyDown() which would recognize all the keys and actual key presses, is still valued.



Solution: using ord()-function to first turn the getch() into an integer (I guess they're virtual key codes, but not too sure) works fine, and then comparing the result to the actual number representing the wanted key. Also, if I needed to, I could add an extra chr() around the number returned so that it would convert it to a character. However, I'm using mostly down arrow, esc, etc. so converting those to a character would be stupid. Here's the final code:



from msvcrt import getch
while True:
key = ord(getch())
if key == 27: #ESC
break
elif key == 13: #Enter
select()
elif key == 224: #Special keys (arrows, f keys, ins, del, etc.)
key = ord(getch())
if key == 80: #Down arrow
moveDown()
elif key == 72: #Up arrow
moveUp()


Also if someone else needs to, you can easily find out the keycodes from google, or by using python and just pressing the key:



from msvcrt import getch
while True:
print(ord(getch()))





share|improve this answer





















  • 3





    Thank you, this was the first answer that worked for me. +1

    – joe_young
    Feb 15 '15 at 9:43






  • 1





    I am using the above code, but my code simply blocks at getch(), and nothing happens then. any help ?

    – Anum Sheraz
    May 9 '16 at 18:48






  • 3





    @AnumSheraz The above method only works when you run the code from command prompt.

    – Moondra
    May 27 '17 at 15:32











  • if you cast a char to bytes first you can compare directly with keypressed 'keypressed == bytes('q', 'utf-8')' checks if q was pressed. It will work for special keys like enter or esc but you need to know the codes for those (esc is 'x1b' for example)

    – Xitcod13
    May 24 '18 at 3:13





















8














See the MSDN getch docs. Specifically:




The _getch and_getwch functions read a single character from the console without echoing the character. None of these functions can be used to read CTRL+C. When reading a function key or an arrow key, each function must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.




The Python function returns a character. you can use ord() to get an integer value you can test, for example keycode = ord(msvcrt.getch()).



So if you read an 0x00 or 0xE0, read it a second time to get the key code for an arrow or function key. From experimentation, 0x00 precedes F1-F10 (0x3B-0x44) and 0xE0 precedes arrow keys and Ins/Del/Home/End/PageUp/PageDown.






share|improve this answer


























  • Well I figured it by now, but can't post the final solution. But this + ord() + char()

    – user1632861
    Aug 29 '12 at 13:26



















1














from msvcrt import getch

pos = [0, 0]

def fright():
global pos
pos[0] += 1

def fleft():
global pos
pos[0] -= 1

def fup():
global pos
pos[1] += 1

def fdown():
global pos
pos[1] -= 1

while True:
print'Distance from zero: ', pos
key = ord(getch())
if key == 27: #ESC
break
elif key == 13: #Enter
print('selected')
elif key == 32: #Space
print('jump')
elif key == 224: #Special keys (arrows, f keys, ins, del, etc.)
key = ord(getch())
if key == 80: #Down arrow
print('down')
fdown
elif key == 72: #Up arrow
print('up')
fup()
elif key == 75: #Left arrow
print('left')
fleft()
elif key == 77: #Right arrow
print('right')
fright()





share|improve this answer



















  • 2





    Please add some explanation to your answer to explain your code.

    – AJ123
    Nov 3 '17 at 16:49












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12175964%2fpython-method-for-reading-keypress%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown
























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









56














Figured it out by testing all the stuff by myself.
Couldn't find any topics about it tho, so I'll just leave the solution here. This might not be the only or even the best solution, but it works for my purposes (within getch's limits) and is better than nothing.



Note: proper keyDown() which would recognize all the keys and actual key presses, is still valued.



Solution: using ord()-function to first turn the getch() into an integer (I guess they're virtual key codes, but not too sure) works fine, and then comparing the result to the actual number representing the wanted key. Also, if I needed to, I could add an extra chr() around the number returned so that it would convert it to a character. However, I'm using mostly down arrow, esc, etc. so converting those to a character would be stupid. Here's the final code:



from msvcrt import getch
while True:
key = ord(getch())
if key == 27: #ESC
break
elif key == 13: #Enter
select()
elif key == 224: #Special keys (arrows, f keys, ins, del, etc.)
key = ord(getch())
if key == 80: #Down arrow
moveDown()
elif key == 72: #Up arrow
moveUp()


Also if someone else needs to, you can easily find out the keycodes from google, or by using python and just pressing the key:



from msvcrt import getch
while True:
print(ord(getch()))





share|improve this answer





















  • 3





    Thank you, this was the first answer that worked for me. +1

    – joe_young
    Feb 15 '15 at 9:43






  • 1





    I am using the above code, but my code simply blocks at getch(), and nothing happens then. any help ?

    – Anum Sheraz
    May 9 '16 at 18:48






  • 3





    @AnumSheraz The above method only works when you run the code from command prompt.

    – Moondra
    May 27 '17 at 15:32











  • if you cast a char to bytes first you can compare directly with keypressed 'keypressed == bytes('q', 'utf-8')' checks if q was pressed. It will work for special keys like enter or esc but you need to know the codes for those (esc is 'x1b' for example)

    – Xitcod13
    May 24 '18 at 3:13


















56














Figured it out by testing all the stuff by myself.
Couldn't find any topics about it tho, so I'll just leave the solution here. This might not be the only or even the best solution, but it works for my purposes (within getch's limits) and is better than nothing.



Note: proper keyDown() which would recognize all the keys and actual key presses, is still valued.



Solution: using ord()-function to first turn the getch() into an integer (I guess they're virtual key codes, but not too sure) works fine, and then comparing the result to the actual number representing the wanted key. Also, if I needed to, I could add an extra chr() around the number returned so that it would convert it to a character. However, I'm using mostly down arrow, esc, etc. so converting those to a character would be stupid. Here's the final code:



from msvcrt import getch
while True:
key = ord(getch())
if key == 27: #ESC
break
elif key == 13: #Enter
select()
elif key == 224: #Special keys (arrows, f keys, ins, del, etc.)
key = ord(getch())
if key == 80: #Down arrow
moveDown()
elif key == 72: #Up arrow
moveUp()


Also if someone else needs to, you can easily find out the keycodes from google, or by using python and just pressing the key:



from msvcrt import getch
while True:
print(ord(getch()))





share|improve this answer





















  • 3





    Thank you, this was the first answer that worked for me. +1

    – joe_young
    Feb 15 '15 at 9:43






  • 1





    I am using the above code, but my code simply blocks at getch(), and nothing happens then. any help ?

    – Anum Sheraz
    May 9 '16 at 18:48






  • 3





    @AnumSheraz The above method only works when you run the code from command prompt.

    – Moondra
    May 27 '17 at 15:32











  • if you cast a char to bytes first you can compare directly with keypressed 'keypressed == bytes('q', 'utf-8')' checks if q was pressed. It will work for special keys like enter or esc but you need to know the codes for those (esc is 'x1b' for example)

    – Xitcod13
    May 24 '18 at 3:13
















56












56








56







Figured it out by testing all the stuff by myself.
Couldn't find any topics about it tho, so I'll just leave the solution here. This might not be the only or even the best solution, but it works for my purposes (within getch's limits) and is better than nothing.



Note: proper keyDown() which would recognize all the keys and actual key presses, is still valued.



Solution: using ord()-function to first turn the getch() into an integer (I guess they're virtual key codes, but not too sure) works fine, and then comparing the result to the actual number representing the wanted key. Also, if I needed to, I could add an extra chr() around the number returned so that it would convert it to a character. However, I'm using mostly down arrow, esc, etc. so converting those to a character would be stupid. Here's the final code:



from msvcrt import getch
while True:
key = ord(getch())
if key == 27: #ESC
break
elif key == 13: #Enter
select()
elif key == 224: #Special keys (arrows, f keys, ins, del, etc.)
key = ord(getch())
if key == 80: #Down arrow
moveDown()
elif key == 72: #Up arrow
moveUp()


Also if someone else needs to, you can easily find out the keycodes from google, or by using python and just pressing the key:



from msvcrt import getch
while True:
print(ord(getch()))





share|improve this answer















Figured it out by testing all the stuff by myself.
Couldn't find any topics about it tho, so I'll just leave the solution here. This might not be the only or even the best solution, but it works for my purposes (within getch's limits) and is better than nothing.



Note: proper keyDown() which would recognize all the keys and actual key presses, is still valued.



Solution: using ord()-function to first turn the getch() into an integer (I guess they're virtual key codes, but not too sure) works fine, and then comparing the result to the actual number representing the wanted key. Also, if I needed to, I could add an extra chr() around the number returned so that it would convert it to a character. However, I'm using mostly down arrow, esc, etc. so converting those to a character would be stupid. Here's the final code:



from msvcrt import getch
while True:
key = ord(getch())
if key == 27: #ESC
break
elif key == 13: #Enter
select()
elif key == 224: #Special keys (arrows, f keys, ins, del, etc.)
key = ord(getch())
if key == 80: #Down arrow
moveDown()
elif key == 72: #Up arrow
moveUp()


Also if someone else needs to, you can easily find out the keycodes from google, or by using python and just pressing the key:



from msvcrt import getch
while True:
print(ord(getch()))






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '17 at 22:29









kres0345

567




567










answered Aug 29 '12 at 14:01







user1632861















  • 3





    Thank you, this was the first answer that worked for me. +1

    – joe_young
    Feb 15 '15 at 9:43






  • 1





    I am using the above code, but my code simply blocks at getch(), and nothing happens then. any help ?

    – Anum Sheraz
    May 9 '16 at 18:48






  • 3





    @AnumSheraz The above method only works when you run the code from command prompt.

    – Moondra
    May 27 '17 at 15:32











  • if you cast a char to bytes first you can compare directly with keypressed 'keypressed == bytes('q', 'utf-8')' checks if q was pressed. It will work for special keys like enter or esc but you need to know the codes for those (esc is 'x1b' for example)

    – Xitcod13
    May 24 '18 at 3:13
















  • 3





    Thank you, this was the first answer that worked for me. +1

    – joe_young
    Feb 15 '15 at 9:43






  • 1





    I am using the above code, but my code simply blocks at getch(), and nothing happens then. any help ?

    – Anum Sheraz
    May 9 '16 at 18:48






  • 3





    @AnumSheraz The above method only works when you run the code from command prompt.

    – Moondra
    May 27 '17 at 15:32











  • if you cast a char to bytes first you can compare directly with keypressed 'keypressed == bytes('q', 'utf-8')' checks if q was pressed. It will work for special keys like enter or esc but you need to know the codes for those (esc is 'x1b' for example)

    – Xitcod13
    May 24 '18 at 3:13










3




3





Thank you, this was the first answer that worked for me. +1

– joe_young
Feb 15 '15 at 9:43





Thank you, this was the first answer that worked for me. +1

– joe_young
Feb 15 '15 at 9:43




1




1





I am using the above code, but my code simply blocks at getch(), and nothing happens then. any help ?

– Anum Sheraz
May 9 '16 at 18:48





I am using the above code, but my code simply blocks at getch(), and nothing happens then. any help ?

– Anum Sheraz
May 9 '16 at 18:48




3




3





@AnumSheraz The above method only works when you run the code from command prompt.

– Moondra
May 27 '17 at 15:32





@AnumSheraz The above method only works when you run the code from command prompt.

– Moondra
May 27 '17 at 15:32













if you cast a char to bytes first you can compare directly with keypressed 'keypressed == bytes('q', 'utf-8')' checks if q was pressed. It will work for special keys like enter or esc but you need to know the codes for those (esc is 'x1b' for example)

– Xitcod13
May 24 '18 at 3:13







if you cast a char to bytes first you can compare directly with keypressed 'keypressed == bytes('q', 'utf-8')' checks if q was pressed. It will work for special keys like enter or esc but you need to know the codes for those (esc is 'x1b' for example)

– Xitcod13
May 24 '18 at 3:13















8














See the MSDN getch docs. Specifically:




The _getch and_getwch functions read a single character from the console without echoing the character. None of these functions can be used to read CTRL+C. When reading a function key or an arrow key, each function must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.




The Python function returns a character. you can use ord() to get an integer value you can test, for example keycode = ord(msvcrt.getch()).



So if you read an 0x00 or 0xE0, read it a second time to get the key code for an arrow or function key. From experimentation, 0x00 precedes F1-F10 (0x3B-0x44) and 0xE0 precedes arrow keys and Ins/Del/Home/End/PageUp/PageDown.






share|improve this answer


























  • Well I figured it by now, but can't post the final solution. But this + ord() + char()

    – user1632861
    Aug 29 '12 at 13:26
















8














See the MSDN getch docs. Specifically:




The _getch and_getwch functions read a single character from the console without echoing the character. None of these functions can be used to read CTRL+C. When reading a function key or an arrow key, each function must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.




The Python function returns a character. you can use ord() to get an integer value you can test, for example keycode = ord(msvcrt.getch()).



So if you read an 0x00 or 0xE0, read it a second time to get the key code for an arrow or function key. From experimentation, 0x00 precedes F1-F10 (0x3B-0x44) and 0xE0 precedes arrow keys and Ins/Del/Home/End/PageUp/PageDown.






share|improve this answer


























  • Well I figured it by now, but can't post the final solution. But this + ord() + char()

    – user1632861
    Aug 29 '12 at 13:26














8












8








8







See the MSDN getch docs. Specifically:




The _getch and_getwch functions read a single character from the console without echoing the character. None of these functions can be used to read CTRL+C. When reading a function key or an arrow key, each function must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.




The Python function returns a character. you can use ord() to get an integer value you can test, for example keycode = ord(msvcrt.getch()).



So if you read an 0x00 or 0xE0, read it a second time to get the key code for an arrow or function key. From experimentation, 0x00 precedes F1-F10 (0x3B-0x44) and 0xE0 precedes arrow keys and Ins/Del/Home/End/PageUp/PageDown.






share|improve this answer















See the MSDN getch docs. Specifically:




The _getch and_getwch functions read a single character from the console without echoing the character. None of these functions can be used to read CTRL+C. When reading a function key or an arrow key, each function must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.




The Python function returns a character. you can use ord() to get an integer value you can test, for example keycode = ord(msvcrt.getch()).



So if you read an 0x00 or 0xE0, read it a second time to get the key code for an arrow or function key. From experimentation, 0x00 precedes F1-F10 (0x3B-0x44) and 0xE0 precedes arrow keys and Ins/Del/Home/End/PageUp/PageDown.







share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 29 '12 at 13:38

























answered Aug 29 '12 at 12:45









Mark TolonenMark Tolonen

96.4k12115177




96.4k12115177













  • Well I figured it by now, but can't post the final solution. But this + ord() + char()

    – user1632861
    Aug 29 '12 at 13:26



















  • Well I figured it by now, but can't post the final solution. But this + ord() + char()

    – user1632861
    Aug 29 '12 at 13:26

















Well I figured it by now, but can't post the final solution. But this + ord() + char()

– user1632861
Aug 29 '12 at 13:26





Well I figured it by now, but can't post the final solution. But this + ord() + char()

– user1632861
Aug 29 '12 at 13:26











1














from msvcrt import getch

pos = [0, 0]

def fright():
global pos
pos[0] += 1

def fleft():
global pos
pos[0] -= 1

def fup():
global pos
pos[1] += 1

def fdown():
global pos
pos[1] -= 1

while True:
print'Distance from zero: ', pos
key = ord(getch())
if key == 27: #ESC
break
elif key == 13: #Enter
print('selected')
elif key == 32: #Space
print('jump')
elif key == 224: #Special keys (arrows, f keys, ins, del, etc.)
key = ord(getch())
if key == 80: #Down arrow
print('down')
fdown
elif key == 72: #Up arrow
print('up')
fup()
elif key == 75: #Left arrow
print('left')
fleft()
elif key == 77: #Right arrow
print('right')
fright()





share|improve this answer



















  • 2





    Please add some explanation to your answer to explain your code.

    – AJ123
    Nov 3 '17 at 16:49
















1














from msvcrt import getch

pos = [0, 0]

def fright():
global pos
pos[0] += 1

def fleft():
global pos
pos[0] -= 1

def fup():
global pos
pos[1] += 1

def fdown():
global pos
pos[1] -= 1

while True:
print'Distance from zero: ', pos
key = ord(getch())
if key == 27: #ESC
break
elif key == 13: #Enter
print('selected')
elif key == 32: #Space
print('jump')
elif key == 224: #Special keys (arrows, f keys, ins, del, etc.)
key = ord(getch())
if key == 80: #Down arrow
print('down')
fdown
elif key == 72: #Up arrow
print('up')
fup()
elif key == 75: #Left arrow
print('left')
fleft()
elif key == 77: #Right arrow
print('right')
fright()





share|improve this answer



















  • 2





    Please add some explanation to your answer to explain your code.

    – AJ123
    Nov 3 '17 at 16:49














1












1








1







from msvcrt import getch

pos = [0, 0]

def fright():
global pos
pos[0] += 1

def fleft():
global pos
pos[0] -= 1

def fup():
global pos
pos[1] += 1

def fdown():
global pos
pos[1] -= 1

while True:
print'Distance from zero: ', pos
key = ord(getch())
if key == 27: #ESC
break
elif key == 13: #Enter
print('selected')
elif key == 32: #Space
print('jump')
elif key == 224: #Special keys (arrows, f keys, ins, del, etc.)
key = ord(getch())
if key == 80: #Down arrow
print('down')
fdown
elif key == 72: #Up arrow
print('up')
fup()
elif key == 75: #Left arrow
print('left')
fleft()
elif key == 77: #Right arrow
print('right')
fright()





share|improve this answer













from msvcrt import getch

pos = [0, 0]

def fright():
global pos
pos[0] += 1

def fleft():
global pos
pos[0] -= 1

def fup():
global pos
pos[1] += 1

def fdown():
global pos
pos[1] -= 1

while True:
print'Distance from zero: ', pos
key = ord(getch())
if key == 27: #ESC
break
elif key == 13: #Enter
print('selected')
elif key == 32: #Space
print('jump')
elif key == 224: #Special keys (arrows, f keys, ins, del, etc.)
key = ord(getch())
if key == 80: #Down arrow
print('down')
fdown
elif key == 72: #Up arrow
print('up')
fup()
elif key == 75: #Left arrow
print('left')
fleft()
elif key == 77: #Right arrow
print('right')
fright()






share|improve this answer












share|improve this answer



share|improve this answer










answered May 15 '17 at 19:32









Tommy LTommy L

8210




8210








  • 2





    Please add some explanation to your answer to explain your code.

    – AJ123
    Nov 3 '17 at 16:49














  • 2





    Please add some explanation to your answer to explain your code.

    – AJ123
    Nov 3 '17 at 16:49








2




2





Please add some explanation to your answer to explain your code.

– AJ123
Nov 3 '17 at 16:49





Please add some explanation to your answer to explain your code.

– AJ123
Nov 3 '17 at 16:49


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12175964%2fpython-method-for-reading-keypress%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks