Skip to content

Instantly share code, notes, and snippets.

@Chunlin-Li
Last active July 21, 2016 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chunlin-Li/9968349572084f9219cc to your computer and use it in GitHub Desktop.
Save Chunlin-Li/9968349572084f9219cc to your computer and use it in GitHub Desktop.
用于自动识别外接键盘的插拔, 并完成笔记本内置键盘的停用和启用
#!/usr/bin/python
__author__ = 'jonny'
import re
import commands
import time
kbid_pattern = re.compile('(?<=id=)\d+', re.I)
masterid_pattern = re.compile('\d+(?=\)\])', re.I)
IN_KB_ID = -1
IN_MASTER_ID = -1
def getCurrenStatus():
global IN_KB_ID, IN_MASTER_ID
output_in = commands.getoutput('xinput list|grep "AT Translated"')
output_ex = commands.getoutput('xinput list|grep "Technology Poker"|tail -n1')
inOk = re.match('.*slave keyboard.*', output_in) is not None
inDis = re.match('.*floating slave.*', output_in) is not None
exOK = re.match('.*slave keyboard.*', output_ex) is not None
exDis = re.match('.*floating slave.*', output_ex) is not None
exNo = len(output_ex) == 0
if inOk and exOK:
# plug in
s = re.search(kbid_pattern, output_in)
IN_KB_ID = s.group(0)
s = re.search(masterid_pattern, output_in)
IN_MASTER_ID = s.group(0)
print('xinput float ' + str(IN_KB_ID))
commands.getoutput('xinput float ' + str(IN_KB_ID))
return 1
elif inOk and exNo:
# initial do nothing
return 0
elif inDis and exOK:
# disabled do nothing
return 2
elif inDis and exNo:
# unplugged
print('xinput reattach ' + str(IN_KB_ID) + ' ' + str(IN_MASTER_ID))
commands.getoutput('xinput reattach ' + str(IN_KB_ID) + ' ' + str(IN_MASTER_ID))
return 3
if __name__ == '__main__':
while True:
currentStatus = getCurrenStatus()
print currentStatus
time.sleep(2)
#!/bin/bash
while true
do
AppleFloat=`xinput |grep -P 'Apple.*floating slave' -c`
echo afloat ${AppleFloat}
AppleID=`xinput | sed -n 's/.*Apple.*id=\([0-9]*\).*/\1/p'`
echo aID ${AppleID}
MasterID=`xinput | sed -n 's/.*id=\([0-9]*\).*master keyboard.*/\1/p'`
echo master ${MasterID}
PokerPresent=`xinput |grep 'Technology Poker' -c`
echo Poker ${PokerPresent}
if [[ $PokerPresent > 0 ]]; then
if [[ $AppleFloat == 0 ]]; then
xinput float ${AppleID}
echo float the Apple keyboard
fi
else
if [[ $AppleFloat > 0 ]];then
xinput reattach ${AppleID} ${MasterID}
echo reattach the Apple keyboard
fi
fi
sleep 2
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment