1

我有一个记录器可以监听方程式等序列,但我需要删除所有按键,如“L_control”或“shift”,我所做的是获取 ascii 编号,然后使用 chr(event.Ascii) 但它又回来了将 ctrl 和 shift 按下作为空格。

我目前正在使用它来删除所有我不想要的字符,但它似乎不起作用。有什么改进的想法吗?

def removeChars(l):
    acceptedChars = ["[", "]", "+", "-", "/", "*", "^", "*", "(", ")"]
    newL = ""
    for x in range(0, len(l)):
        if l[x].isalpha() or l[x] in acceptedChars or l[x].isdigit():
            newL = newL + l[x]
    return newL

编辑:

pyHook用于获取关键事件并event.Ascii用于获取 Ascii 值,然后chr(event.Ascii)用于获取字符

4

1 回答 1

2

这样的事情呢?

import string
acceptedChars = set(string.printable)
newL = ''.join([ x for x in l if x in acceptedChars])

编辑:

您可以使用任何内容set()进行匹配,例如仅获取数字、字母和选定的符号:

acceptedChars = set(string.digits + "[]()+-/*^=!<>" + string.letters)
newL = ''.join([ x for x in l if x in acceptedChars])
于 2012-08-14T22:00:25.060 回答