bash or perl: toggle internal keyboard script - bash

Not very familiar with perl or bash scripting, trying to write a script to toggle internal keyboard (on laptop using external mechanical keyboard).
Toggle internal keyboard is easy enough to do with the xinput command:
$ 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)]
Identify id and float number of AT Translated Set 2 keyboard (built in keyboard).
$ 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]
To reattach keyboard use:
$ xinput reattach 13 3
No major issues here, just curious about writing a script to do this automatically, so far I have some mock up code which goes like this:
#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
I want to translate this into a bash or perl script, something easy to run without jumping through any hoops from my shell.
Either, what might be a good online resource, or resources, for me to reference while creating the proper syntax for this?
Or how would a functional version of this script look like?

I don't know the output of xinput, but based on the assumption that it delivers lines like you provided, it would be something along these lines (no way to test it now):
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]);
}
This of course assumes that your search term is always found. No error handling there...

Related

MacOS is scrambling bluetooth PINs if they contain char values greater than 0x7E

I'm trying connect a MacOS Monterey 12.3 (M1, 2020) laptop to a bluetooth device typically intended for embedded applications.
The device's bluetooth PIN is the device's address and therefore has char values in the range 0x00 to 0xFF.
If I enter a PIN into the System Preferences > Bluetooth settings GUI such as 0000, I can see the same pin in Xcode PacketLogger (included in Additional Tools for Xcode):
PIN Code Length: 0x04
PIN Code: 0000 (0x30 0x30 0x30 0x30)
However if I send a code such as ¡¡¡¡ ( equivalent to [0xA1, 0xA1, 0xA1, 0xA1]) then in PacketLogger I see:
PIN Code Length: 0x06
PIN Code: ¡¡¡ (0xC2 0xA1 0xC2 0xA1 0xC2 0xA1)
To enter a pin of ¡¡¡¡ I've tried three approaches:
Converting and copy-pasting from an online tool such as RapidTables
Converting in a python terminal and copy-pasting
Setting the Keyboard to "Unicode Hex Input" and directly typing it in by holding down option and typing 00A1
I'm new to MacOS so not sure if its quirk about how it handles text encodings. Can someone explain to me what I'm seeing?

How to re-configure mouse buttons on Linux?

I want to re-configure mouse settings so that I could scroll up and down when I click the middle button on my mouse (wheel).
I typed the xinput --list command in the terminal and got the following output:
⎡ Virtual core pointer id=2 [master pointer (3)]
⎜ ↳ Virtual core XTEST pointer id=4 [slave pointer (2)]
⎜ ↳ xwayland-pointer:16 id=6 [slave pointer (2)]
⎜ ↳ xwayland-relative-pointer:16 id=7 [slave pointer (2)]
⎣ Virtual core keyboard id=3 [master keyboard (2)]
↳ Virtual core XTEST keyboard id=5 [slave keyboard (3)]
↳ xwayland-keyboard:16 id=8 [slave keyboard (3)]
What was strange that there were no mouse or keyboard in the list. Is there any way to achieve my goal?
OS: Fedora 31.

Ignore physical keyboard state when using SendInput

So I'm currently using SendInput as a way of doing keyboard emulation. My problem is that to switch on the emulation the user has to hold down the CTRL button (client's specification :-( ).
I used GetAsyncKeyState(VK_CONTROL) to get the state of the CTRL key, so I can work out the switch part. My problem now is that since the physical CTRL button is pressed, it is not ignored when the SendInput function is used. So instead of emulating say the c button it emulates CTRL + c.
I'd like to know if there is a way for SendInput to ignore physical keyboard presses.

Capturing shift key state on Windows 8 tablet in MFC App

Our MFC-based Windows application has functionality that depends on the state of the shift key. Shift exposes power-user menu commands and shift-dragging has different behavior than normal dragging. I am exploring our options in supporting Windows 8 tablets, and it seems that there is no straightforward way to detect the state of the Windows 8 soft keypad shift key.
Using GetAsyncKeyState(VK_SHIFT) (our current method) doesn't detect this key.
Experimenting with examining all incoming keyboard messages, I find:
A single isolated tap of the shift key produces no keyboard event
A double tap of the shift key produces a caps lock key event, indistinguishable from a normal caps lock key event
A single tap of the shift key followed by another keystroke (eg. the 'q' key) produces a shift key event, the keystroke, and a shift key (up?) event.
Are there any other ways I could try to detect the state of the soft keyboard shift key? Has anyone else had success detecting the state of this keypad on Windows 8 tablet soft keyboards.
The external keyboards that you plug into these devices all have normal behaving shift keys. The problem is specific to the soft keypad.

keyboard transmit mode in vt100 terminal emulator

I'm implementing a vt100 terminal emulator in javascript and the vt100 spec (from man terminfo and infocmp) tells me that smkx=\E[?1h\E= is the code to enter key-board transmit mode and rmkx=\E[?1l\E> is the code to leave 'key-board transmit' mode. I couldn't really understand what key-board transmit mode is.
From http://vt100.net/dec/ek-vt220-tm-001.pdf,
7.4.4.1 Keyboard Transmit Mode -- The keyboard codes and a few other special codes are transmitted via a serial line output in PORT of the
8051. The transmitted signal goes from the 8051 to a driver, through the keyboard cable, monitor and video cable to the CPU. A UART within
the 8051 controls the transmission"
I'm getting these codes while entering into and leaving vim, respectively. Can I safely ignore these codes or do I need to handle them? In the latter case, what am I supposed to do?
These two strings contain two sequences each; they control subtly different things:
The first of these,
CSI ? 1h
CSI ? 1l
controls DEC private mode number 1, DECCKM, cursor key mode. This mode determines whether the four arrow keys (Up, Down, Left, Right) will send SS3 or CSI-prefixed sequences.
The second pair of sequences,
ESC =
ESC >
are called DECKPAM, keypad application mode, and DECKPNM, keypad numeric mode. This determines whether the number keypad sends control sequences, or normal numbers (i.e. consider it similar to the Numlock feature).
smkx/rmkx control whether the keypad will transmit numbers or escape code. Also, it is "keypad-transmit-mode" not "keyboard-transmit-mode"

Resources