Py: Difference between revisions
Jump to navigation
Jump to search
(Python page for stuff #ff6600) |
m (Added sampler code (currently untested) (16:58)) |
||
| Line 19: | Line 19: | ||
== Downloads == | == Downloads == | ||
https://www.python.org/downloads/ | https://www.python.org/downloads/ | ||
*Note on windows a system update may first be required to prevent installation errors. | |||
== Docs == | == Docs == | ||
https://www.python.org/doc/ | https://www.python.org/doc/ | ||
== Projects == | |||
=== Socket to Keys === | |||
Original code from Sergio http://sergiosprojects.blogspot.com/2012/01/python-server-keyboard-emulation.html | |||
<pre> | |||
#!/usr/bin/env python | |||
import os | |||
import sys | |||
import socket | |||
import ctypes | |||
# Used to listen for connections | |||
LISTEN_ADDRESS = '' | |||
LISTEN_PORT = 12345 | |||
BUFFER_SIZE = 512 | |||
# Used for emulating key presses. | |||
user32 = ctypes.windll.user32 | |||
# Virtual keys | |||
VK_VOLUME_MUTE = 0xAD | |||
VK_VOLUME_DOWN = 0xAE | |||
VK_VOLUME_UP = 0xAF | |||
VK_MEDIA_NEXT_TRACK = 0xB0 | |||
VK_MEDIA_PREV_TRACK = 0xB1 | |||
VK_MEDIA_STOP = 0xB2 | |||
VK_MEDIA_PLAY_PAUSE = 0xB3 | |||
# Mapping expected input to actions | |||
bytes_to_keys = {} | |||
bytes_to_keys["1"] = VK_MEDIA_PLAY_PAUSE | |||
bytes_to_keys["2"] = VK_MEDIA_NEXT_TRACK | |||
bytes_to_keys["3"] = VK_MEDIA_PREV_TRACK | |||
bytes_to_keys["4"] = VK_MEDIA_STOP | |||
def run_command(cmd): | |||
os.system(cmd) | |||
def do_action(char): | |||
'''Takes a 1-character code and executes the action assoliated with it''' | |||
if char in bytes_to_keys: | |||
key_code = bytes_to_keys[char] | |||
user32.keybd_event(key_code, 0, 0, 0) # press | |||
user32.keybd_event(key_code, 0, 2, 0) # release | |||
else: | |||
print("unknown instruction:", char) | |||
########################################## | |||
################## Main ################## | |||
########################################## | |||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |||
s.bind((LISTEN_ADDRESS, LISTEN_PORT)) | |||
print("Starting server on port", LISTEN_PORT) | |||
s.listen(1) | |||
while True: | |||
conn, addr = s.accept() | |||
print('Connection address:', addr) | |||
try: | |||
while True: | |||
data = conn.recv(BUFFER_SIZE) | |||
if not data: break | |||
str_data = bytes.decode(data) # data is non-unicode, but all python strings need to be | |||
print("received data: \"{0}\"".format(str_data)) | |||
for char in str_data: # highly unlikely, but crazier things have happened | |||
do_action(char) | |||
print("Client disconnected") | |||
except socket.error as e: | |||
print("Socket error:", e) | |||
except socket.timeout: | |||
print("Connection timed-out") | |||
except Exception as e: | |||
print("Exception:", e) | |||
finally: | |||
conn.close() | |||
print("Exit") | |||
</pre> | |||
Revision as of 16:58, 20 October 2015
,-.----.
\ / \ ___ ,---,
| : \ ,--.'|_ ,--.' |
| | .\ : | | :,' | | : ,---. ,---,
. : |: | : : ' : : : : ' ,'\ ,-+-. / |
| | \ : .--,.;__,' / : | |,--. / / | ,--.'|' |
| : . / /_ ./|| | | | : ' |. ; ,. :| | ,"' |
; | |`-', ' , ' ::__,'| : | | /' :' | |: :| | / | |
| | ; /___/ \: | ' : |__ ' : | | |' | .; :| | | | |
: ' | . \ ' | | | '.'|| | ' | :| : || | | |/
: : : \ ; : ; : ;| : :_:,' \ \ / | | |--'
| | : \ \ ; | , / | | ,' `----' | |/
`---'.| : \ \ ---`-' `--'' '---'
`---` \ ' ;
`--`
Downloads
https://www.python.org/downloads/
- Note on windows a system update may first be required to prevent installation errors.
Docs
Projects
Socket to Keys
Original code from Sergio http://sergiosprojects.blogspot.com/2012/01/python-server-keyboard-emulation.html
#!/usr/bin/env python
import os
import sys
import socket
import ctypes
# Used to listen for connections
LISTEN_ADDRESS = ''
LISTEN_PORT = 12345
BUFFER_SIZE = 512
# Used for emulating key presses.
user32 = ctypes.windll.user32
# Virtual keys
VK_VOLUME_MUTE = 0xAD
VK_VOLUME_DOWN = 0xAE
VK_VOLUME_UP = 0xAF
VK_MEDIA_NEXT_TRACK = 0xB0
VK_MEDIA_PREV_TRACK = 0xB1
VK_MEDIA_STOP = 0xB2
VK_MEDIA_PLAY_PAUSE = 0xB3
# Mapping expected input to actions
bytes_to_keys = {}
bytes_to_keys["1"] = VK_MEDIA_PLAY_PAUSE
bytes_to_keys["2"] = VK_MEDIA_NEXT_TRACK
bytes_to_keys["3"] = VK_MEDIA_PREV_TRACK
bytes_to_keys["4"] = VK_MEDIA_STOP
def run_command(cmd):
os.system(cmd)
def do_action(char):
'''Takes a 1-character code and executes the action assoliated with it'''
if char in bytes_to_keys:
key_code = bytes_to_keys[char]
user32.keybd_event(key_code, 0, 0, 0) # press
user32.keybd_event(key_code, 0, 2, 0) # release
else:
print("unknown instruction:", char)
##########################################
################## Main ##################
##########################################
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((LISTEN_ADDRESS, LISTEN_PORT))
print("Starting server on port", LISTEN_PORT)
s.listen(1)
while True:
conn, addr = s.accept()
print('Connection address:', addr)
try:
while True:
data = conn.recv(BUFFER_SIZE)
if not data: break
str_data = bytes.decode(data) # data is non-unicode, but all python strings need to be
print("received data: \"{0}\"".format(str_data))
for char in str_data: # highly unlikely, but crazier things have happened
do_action(char)
print("Client disconnected")
except socket.error as e:
print("Socket error:", e)
except socket.timeout:
print("Connection timed-out")
except Exception as e:
print("Exception:", e)
finally:
conn.close()
print("Exit")