From: Adrian Iain Lam Date: Wed, 24 Aug 2016 20:15:26 +0000 (+0800) Subject: Initial commit X-Git-Tag: v0.99~7 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=fc1c079170ce7a35bd5c097f6d9fde70ba575be4;p=indicator-keyboard-led.git Initial commit --- fc1c079170ce7a35bd5c097f6d9fde70ba575be4 diff --git a/README.md b/README.md new file mode 100644 index 0000000..86240b9 --- /dev/null +++ b/README.md @@ -0,0 +1,86 @@ +# indicator-keyboard-led - simulate keyboard lock keys LED + +This is a Unity application indicator designed for keyboards without lock +keys LED. It allows the user to check the state of the three locks (Caps lock, +Num lock and Scroll lock) without requiring any mouse or keyboard action. It +also allows the lock keys to be toggled with mouse clicks, which could be +useful for keyboards without Scroll lock keys or malfunctioning keyboards. + +## Screenshots + +![indicator default][sc1] +Default appearance of the indicator with Num lock on and Caps and Scroll locks +off. + +![indicator menu][sc2] +Menu of the indicator, shown on click. The locks can be toggled by clicking +the respective item in the menu. + +![indicator short][sc3] +Alternative (short) appearance of the indicator. + +[sc1]: screenshots/sc1.png +[sc2]: screenshots/sc2.png +[sc3]: screenshots/sc3.png + +## Dependencies + - Python 3 (*) + - GTK+ 3 (*) + - AppIndicator 3 (*) + - Python 3 GObject introspection (python3-gi) + - xdotool + +Those marked with (*) are probably installed by default in recent Ubuntu +distributions. To install the rest, run: + + sudo apt-get install python3-gi xdotool + +## Usage + + 1. Install the dependencies listed above. + 2. Clone this repository. + 3. Add the script as a startup application. (Use option `--short` for short + appearance if desired.) + 4. Run the script manually for the first time. (Alternatively, log out + and log in again.) + 5. The indicator should be shown at the top right corner, with a filled circle + representing a lock turned on and an unfilled circle representing a lock + turned off. + 6. Clicking on the indicator should result in a menu with the three locks. + Clicking on the menu item would cause the corresponding lock to toggle. + +## License + +The program "indicator-keyboard-led.py" is released under the MIT License. +Please refer to the file for the full text of the license. + +The icon "icon.svg" is released to the public domain. + +## Credits + +I would like to thank [Tobias Schlitt](https://github.com/tobyS), who wrote +[indicator-chars](https://github.com/tobyS/indicator-chars) which I used +as a reference when writing this software. + +The icon used in the indicator (icon.svg) is modified from the file +"emblem-readonly.svg" by [Jakub Steiner](http://jimmac.musichall.cz) +who released it to the public domain for the +[Tango Icon Library](http://tango.freedesktop.org/Tango_Icon_Library). + +--- + +## Motivation + +I was a user of [indicator-keylock][ind-kl], but only one key lock can be shown +on the panel. I didn't like the Notify OSD events either. + +I then came across [lks-indicator][lks] and [indicator-xbdmod][xbdmod], but +I didn't like the fact that they refresh the indicator on a regular time +interval (every *x* milliseconds) rather than on state changes (only when +the locks are toggled). + +I also thought it would be fun to be able to toggle the locks on-screen. + +[ind-kl]: https://launchpad.net/~tsbarnes/+archive/ubuntu/indicator-keylock +[lks]: https://github.com/SergKolo/lks-indicator +[xbdmod]: https://github.com/sneetsher/indicator-xkbmod diff --git a/icon.svg b/icon.svg new file mode 100644 index 0000000..d711e58 --- /dev/null +++ b/icon.svg @@ -0,0 +1,88 @@ + + + + + + + image/svg+xml + + + + Jakub Steiner + + + Adrian I Lam + http://jimmac.musichall.cz + + Caps Lock icon + caps lock + + + + + + + + + + + + diff --git a/indicator-keyboard-led.py b/indicator-keyboard-led.py new file mode 100755 index 0000000..44e02a0 --- /dev/null +++ b/indicator-keyboard-led.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# indicator-keyboard-led - simulate keyboard lock keys LED +# Copyright (c) 2016 Adrian I Lam +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHOR OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +# IN THE SOFTWARE. +# +# I would like to thank Tobias Schlitt , who wrote +# indicator-chars which I used +# as a reference when writing this software. + +import signal +import subprocess +from os import path +import argparse +import gi +gi.require_version('Gdk', '3.0') +gi.require_version('Gtk', '3.0') +gi.require_version('AppIndicator3', '0.1') +from gi.repository import Gdk, Gtk, AppIndicator3 + +class IndicatorKeyboardLED: + locks = { 'caps': 'Caps', 'num': 'Num', 'scr': 'Scroll' } + + def __init__(self, short=False): + self.indicator = AppIndicator3.Indicator.new( + 'indicator-keyboard-led', + path.join(path.dirname(path.realpath(__file__)), 'icon.svg'), + AppIndicator3.IndicatorCategory.APPLICATION_STATUS) + self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE) + + if short: + self.locks = { 'caps': 'C', 'num': 'N', 'scr': 'S' } + + keymap = Gdk.Keymap.get_default() + keymap.connect('state-changed', self.update_indicator) + self.update_indicator(keymap) + + menu = Gtk.Menu() + items = { + 'caps' : Gtk.MenuItem.new_with_label('Caps Lock'), + 'num' : Gtk.MenuItem.new_with_label('Num Lock'), + 'scr' : Gtk.MenuItem.new_with_label('Scroll Lock') + } + menu.append(items['caps']) + menu.append(items['num']) + menu.append(items['scr']) + + items['caps'].connect('activate', self.send_keypress, 'Caps_Lock') + items['num' ].connect('activate', self.send_keypress, 'Num_Lock') + items['scr' ].connect('activate', self.send_keypress, 'Scroll_Lock') + + self.indicator.set_menu(menu) + menu.show_all() + + def update_indicator(self, keymap): + label = '⚫' if keymap.get_caps_lock_state() else '⚪' + label += self.locks['caps'] + ' ' + label += '⚫' if keymap.get_num_lock_state() else '⚪' + label += self.locks['num'] + ' ' + label += '⚫' if keymap.get_scroll_lock_state() else '⚪' + label += self.locks['scr'] + self.indicator.set_label(label, '') + + def send_keypress(self, menuitem, keystroke): + subprocess.call(['xdotool', 'key', keystroke]) + +if __name__ == '__main__': + signal.signal(signal.SIGINT, lambda signum, frame: Gtk.main_quit()) + + parser = argparse.ArgumentParser( + description='indicator-keyboard-led - simulate keyboard lock keys LED') + parser.add_argument('-s', '--short', dest='short', action='store_true', + help='use short label, i.e. ⚫C ⚫N ⚫S instead of ⚫Caps ⚫Num ⚫Scroll', + required=False) + args = parser.parse_args() + + IndicatorKeyboardLED(short=args.short) + Gtk.main() diff --git a/screenshots/sc1.png b/screenshots/sc1.png new file mode 100644 index 0000000..394422e Binary files /dev/null and b/screenshots/sc1.png differ diff --git a/screenshots/sc2.png b/screenshots/sc2.png new file mode 100644 index 0000000..da9d3b5 Binary files /dev/null and b/screenshots/sc2.png differ diff --git a/screenshots/sc3.png b/screenshots/sc3.png new file mode 100644 index 0000000..2424054 Binary files /dev/null and b/screenshots/sc3.png differ