You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python生成逼真的类键盘随机垃圾字符串?

生成类键盘垃圾字符串的Python实现

核心思路

不用逐个标记按键位置,通过按键权重分配和邻接按键关联来模拟真实键盘误输入的垃圾内容,既符合需求又高效。

具体实现步骤

  • 定义键盘布局与权重
    把键盘分为主行(asdfghjkl;)、上行(qwertyuiop[])、下行(zxcvbnm,./),主行按键的选中概率最高,手指默认放置的基准键(a/s/d/f/j/k/l/;)额外提升权重,对应字典示例:

    key_weights = {
        # 基准键(权重4)
        'a':4, 's':4, 'd':4, 'f':4,
        'j':4, 'k':4, 'l':4, ';':4,
        # 主行其他键(权重3)
        'g':3, 'h':3,
        # 上行按键(权重2)
        'q':2, 'w':2, 'e':2, 'r':2, 't':2, 'y':2, 'u':2, 'i':2, 'o':2, 'p':2, '[':2, ']':2,
        # 下行按键(权重1)
        'z':1, 'x':1, 'c':1, 'v':1, 'b':1, 'n':1, 'm':1, ',':1, '.':1, '/':1
    }
    
  • 定义按键邻接关系
    为每个按键列出物理相邻的按键,模拟连续按到相邻键的情况:

    adjacent_keys = {
        'a': ['q', 'w', 's', 'z'],
        's': ['a', 'd', 'w', 'e', 'x'],
        'd': ['s', 'f', 'e', 'r', 'c'],
        'f': ['d', 'g', 'r', 't', 'v'],
        'g': ['f', 'h', 't', 'y', 'b'],
        'h': ['g', 'j', 'y', 'u', 'n'],
        'j': ['h', 'k', 'u', 'i', 'm'],
        'k': ['j', 'l', 'i', 'o', ','],
        'l': ['k', ';', 'o', 'p', '.'],
        ';': ['l', 'p', '[', '/'],
        'q': ['a', 'w'],
        'w': ['q', 'e', 'a', 's'],
        'e': ['w', 'r', 's', 'd'],
        'r': ['e', 't', 'd', 'f'],
        't': ['r', 'y', 'f', 'g'],
        'y': ['t', 'u', 'g', 'h'],
        'u': ['y', 'i', 'h', 'j'],
        'i': ['u', 'o', 'j', 'k'],
        'o': ['i', 'p', 'k', 'l'],
        'p': ['o', '[', 'l', ';'],
        '[': ['p', ']', ';'],
        ']': ['['],
        'z': ['a', 'x'],
        'x': ['z', 'c', 'a', 's'],
        'c': ['x', 'v', 's', 'd'],
        'v': ['c', 'b', 'd', 'f'],
        'b': ['v', 'n', 'f', 'g'],
        'n': ['b', 'm', 'g', 'h'],
        'm': ['n', ',', 'h', 'j'],
        ',': ['m', '.', 'j', 'k'],
        '.': [',', '/', 'k', 'l'],
        '/': ['.', ';']
    }
    
  • 实现生成函数
    使用random.choices()根据权重选字符,每一步70%概率从当前字符的邻接键中选择,30%概率从所有按键中随机选:

    import random
    
    def generate_keyboard_garbage(length):
        if length <= 0:
            return ""
        
        # 初始化按键列表和权重列表
        keys = list(key_weights.keys())
        weights = list(key_weights.values())
        
        # 生成第一个字符
        current_char = random.choices(keys, weights=weights, k=1)[0]
        garbage = [current_char]
        
        for _ in range(length-1):
            # 70%概率选相邻键,30%概率随机选所有键
            if random.random() < 0.7:
                # 从相邻键中按权重筛选
                adj_keys = adjacent_keys[current_char]
                adj_weights = [key_weights[k] for k in adj_keys]
                next_char = random.choices(adj_keys, weights=adj_weights, k=1)[0]
            else:
                next_char = random.choices(keys, weights=weights, k=1)[0]
            
            garbage.append(next_char)
            current_char = next_char
        
        return ''.join(garbage)
    
    # 测试:生成长度为20的垃圾字符串
    print(generate_keyboard_garbage(20))
    

可调整的参数

  • 修改random.random() < 0.7中的0.7,可调整相邻按键的选中概率,数值越高越容易连续输入相邻键
  • 修改key_weights中的权重值,可调整不同区域按键的选中概率

内容的提问来源于stack exchange,提问作者JohnTheJohn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 08:06:18