bash或perl:切换内部键盘脚本

时间:2017-01-26 20:33:53

标签: bash perl keyboard

不熟悉perl或bash脚本,尝试编写脚本来切换内部键盘(在笔记本电脑上使用外部机械键盘)。

使用xinput命令轻松切换内部键盘:

$ xinput
    ⎡ Virtual core pointer                      id=2    [master pointer  (3)]
    ⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
    ⎜   ↳ Elan Touchpad                             id=11   [slave  pointer  (2)]
    ⎜   ↳ SINO WEALTH USB KEYBOARD                  id=15   [slave  pointer  (2)]
    ⎣ Virtual core keyboard                     id=3    [master keyboard (2)]
        ↳ Virtual core XTEST keyboard               id=5    [slave  keyboard (3)]
        ↳ Power Button                              id=6    [slave  keyboard (3)]
        ↳ Video Bus                                 id=7    [slave  keyboard (3)]
        ↳ Video Bus                                 id=8    [slave  keyboard (3)]
        ↳ Sleep Button                              id=9    [slave  keyboard (3)]
        ↳ USB2.0 VGA UVC WebCam                     id=10   [slave  keyboard (3)]
        ↳ Asus WMI hotkeys                          id=12   [slave  keyboard (3)]
        ↳ AT Translated Set 2 keyboard              id=13   [slave  keyboard (3)]
        ↳ SINO WEALTH USB KEYBOARD                  id=14   [slave  keyboard (3)]

识别AT Translated Set 2键盘(内置键盘)的ID和浮点数。

$ xinput float 13
$ xinput
⎡ Virtual core pointer                      id=2    [master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
⎜   ↳ Elan Touchpad                             id=11   [slave  pointer  (2)]
⎜   ↳ SINO WEALTH USB KEYBOARD                  id=15   [slave  pointer  (2)]
⎣ Virtual core keyboard                     id=3    [master keyboard (2)]
    ↳ Virtual core XTEST keyboard               id=5    [slave  keyboard (3)]
    ↳ Power Button                              id=6    [slave  keyboard (3)]
    ↳ Video Bus                                 id=7    [slave  keyboard (3)]
    ↳ Video Bus                                 id=8    [slave  keyboard (3)]
    ↳ Sleep Button                              id=9    [slave  keyboard (3)]
    ↳ USB2.0 VGA UVC WebCam                     id=10   [slave  keyboard (3)]
    ↳ Asus WMI hotkeys                          id=12   [slave  keyboard (3)]
    ↳ SINO WEALTH USB KEYBOARD                  id=14   [slave  keyboard (3)]
∼ AT Translated Set 2 keyboard              id=13   [floating slave]

重新连接键盘:

$ xinput reattach 13 3

这里没有重大问题,只是好奇编写一个脚本来自动执行此操作,到目前为止,我有一些模拟代码,如下所示:

#bin/bash

val string = xinput | grep "AT keyboard"

if(parse(string).attached)
    val floatPartNumber = parse(string).id
    val reattachNumber = parse(string).slaveNumber

    # float keyboard
    xinput float floatPartNumber

    # create hidden file for storage
    file = makeFile(".reattachData")

    #add needed data to file
    writeTOFile(file)(floatPartNumber)
    writeTOFile(file)(reattachNumber)
fi
else
    val file = openFile(".reattachData") 
    val floatPartNumber = file.lineOne
    val reattachNumber = file.lineTwo

    # reattach keyboard
    xinput reattach floatPartNumber reattachNumber
end

我想把它翻译成一个bash或perl脚本,这个东西很容易运行而不会从我的shell跳过任何箍。

要么在为此创建正确的语法时引用可能是一个很好的在线资源或资源? 或者这个脚本的功能版本怎么样?

1 个答案:

答案 0 :(得分:0)

我不知道xinput的输出,但是基于它提供了像你提供的那样的线条的假设,它将是这些线上的东西(现在无法测试它):

use strict;
use warnings;

my $search = 'AT Translated Set 2 keyboard';
my @xout = qx(xinput); # or use backticks instead of qx

my ($id,$slave) = map { # search for match and capture the numbers
    /$search.*id=(\d+)\s+\[(?:floating slave|slave  keyboard \((\d+)\))\]/
    ? ($1,$2) : ()
    } @xout;

if ( defined($slave) ) { # should be undef in case of 'floating slave'
    if (open(my $fh, '>', '.reattachData')) {
        $fh->print($id.$/.$slave);
        $fh->close;
    }
    qx(xinput float $id);
}
else {
    my @line = do { local(@ARGV) = '.reattachData';(<>) }; # read file
    chomp(@line);
    qx(xinput reattach $line[0] $line[1]);  
}

这当然假设您的搜索字词始终存在。那里没有错误处理...