3
ÖŒ7]Ê  ã               @   s    d Z ddlZejdkr ddlZn$ddlZddlZddlZddlmZ G dd„ dƒZe	dkrœeƒ Z
edƒ x*e
jƒ rje
jƒ Zeeƒd	krˆP eeƒ qjW e
jƒ  dS )
a‡  
A Python class implementing KBHIT, the standard keyboard-interrupt poller.
Works transparently on Windows and Posix (Linux, Mac OS X).  Doesn't work
with IDLE.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as 
published by the Free Software Foundation, either version 3 of the 
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

é    NÚnt)Úselectc               @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚKBHitc             C   sx   t jdkrnhtjjƒ | _tj| jƒ| _tj| jƒ| _	| jd tj
 @ tj @ | jd< tj| jtj| jƒ tj| jƒ dS )zPCreates a KBHit object that you can call to do various keyboard things.
        r   é   N)ÚosÚnameÚsysÚstdinÚfilenoÚfdÚtermiosZ	tcgetattrÚnew_termÚold_termZICANONZECHOÚ	tcsetattrÚ	TCSAFLUSHÚatexitÚregisterÚset_normal_term)Úself© r   úE/cygdrive/c/Users/henthorn/Documents/Arduino/EyeRISUDPServer/kbhit.pyÚ__init__#   s    
 zKBHit.__init__c             C   s$   t jdkrntj| jtj| jƒ dS )zA Resets to normal terminal.  On Windows this is a no-op.
        r   N)r   r   r   r   r   r   r   )r   r   r   r   r   9   s    
zKBHit.set_normal_termc             C   s$   t jdkrntj| jtj| jƒ dS )zj Resets back to new terminal.  Useful after using set_normal_term().  On Windows this is a no-op.
        r   N)r   r   r   r   r   r   r   )r   r   r   r   Úset_new_termD   s    
zKBHit.set_new_termc             C   s,   d}t jdkrtjƒ jdƒS tjjdƒS dS )z‰ Returns a keyboard character after kbhit() has been called.
            Should not be called in the same program as getarrow().
        Ú r   zutf-8é   N)r   r   ÚmsvcrtÚgetchÚdecoder   r	   Úread)r   Úsr   r   r   r   O   s    
zKBHit.getchc             C   sX   t jdkr(tjƒ  tjƒ }ddddg}ntjjdƒd }dd	d
dg}|jt|j	dƒƒƒS )zÌ Returns an arrow-key code after kbhit() has been called. Codes are
        0 : up
        1 : right
        2 : down
        3 : left
        Should not be called in the same program as getch().
        r   éH   éM   éP   éK   r   é   éA   éC   éB   éD   zutf-8)
r   r   r   r   r   r	   r   ÚindexÚordr   )r   ÚcÚvalsr   r   r   Úgetarrow]   s    	
zKBHit.getarrowc             C   s6   t jdkrtjƒ S ttjgg g dƒ\}}}|g kS dS )zF Returns True if keyboard character was hit, False otherwise.
        r   r   N)r   r   r   Úkbhitr   r   r	   )r   ZdrZdwZder   r   r   r.   r   s    
zKBHit.kbhitN)	Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r-   r.   r   r   r   r   r   !   s   r   Ú__main__zHit any key, or ESC to exité   )Ú__doc__r   r   r   r   r   r   r   r   r/   ÚkbÚprintr.   r   r+   r*   r   r   r   r   r   Ú<module>   s$   

]