Keyboard input/Keypress check: Difference between revisions

Content added Content deleted
No edit summary
(→‎{{header|Python}}: someone should test, if still working also in windows environment and prior implementation 'char.decode('utf-8')' throws error when using german 'Ä,Ö,Ü,ä,ö,ü')
Line 443: Line 443:
=={{header|Python}}==
=={{header|Python}}==
<lang python>#!/usr/bin/env python
<lang python>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, unicode_literals, print_function


import __future__
import sys
import sys
if sys.version_info.major < 3:
if sys.version_info.major < 3:
Line 466: Line 467:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
return ch

char = None


def keypress():
def keypress():
Line 473: Line 472:
char = getch()
char = getch()


def main():
_thread.start_new_thread(keypress, ())
global char
char = None
_thread.start_new_thread(keypress, ())

while True:
if char is not None:
print("Key pressed is " + char.decode('utf-8'))
_thread.start_new_thread(keypress, ())
if char == 'q' or char == '\x1b': # x1b is ESC
exit()
char = None
print("Program is running")
time.sleep(1)</lang>


if __name__ == "__main__":
while True:
main()
if char is not None:
print("Key pressed is " + char.decode('utf-8'))
break
print("Program is running")
time.sleep(5)</lang>


=={{header|PureBasic}}==
=={{header|PureBasic}}==