Code Monkey home page Code Monkey logo

keyboard_layout's Introduction

Keyboard switcher for Awesome WM with additional layouts

Intro

Usually I use two keyboard layouts on my computer there are English and Russian layouts. But sometimes I need to write something on another language, for example: German. But adding new keyboard layout was very annoying me. Because I have to press more than one time for changing my keyboard layout from Russian to English. But with this widget I can configure additional layouts. When I press on buttons on keyboard only my primary layouts will be switching. But when I want to use additional layout, I just click right mouse button on keyboard widget and select one of additional layouts. You can see how it is work in the Screenshot section.

Table of Contents

Features

  • Conception of additional layouts
  • Flexible configuration of the widget
  • GUI/TUI modes of display current layout.
  • Possibility of remembering layout per window.

Installing

Clone this repository to your Awesome WM configuration directory:

cd ~/.config/awesome
git clone https://github.com/echuraev/keyboard_layout

Usage

  1. Add call of keyboard_layout module to your rc.init:

    local keyboard_layout = require("keyboard_layout")
  2. Create instance of keyboard widget (full information about options that are supported, you can find in section: options). You can choose between text and graphical layout label, see below. Primary and Additional layouts can be set by add_primary_layout and add_additional_layout respectively. And when you add all necessary options to kbdcfg then you have to call bind functions. In this call all your settings will apply.

    2.1. Create text label:

    local kbdcfg = keyboard_layout.kbdcfg({type = "tui"})
    
    kbdcfg.add_primary_layout("English", "US", "us")
    kbdcfg.add_primary_layout("Русский", "RU", "ru")
    
    kbdcfg.add_additional_layout("Deutsch",  "DE", "de")
    kbdcfg.add_additional_layout("Français", "FR", "fr")
    kbdcfg.bind()

    2.2. Create graphical label:

    local kbdcfg = keyboard_layout.kbdcfg({type = "gui"})
    
    kbdcfg.add_primary_layout("English", beautiful.en_layout, "us")
    kbdcfg.add_primary_layout("Русский", beautiful.ru_layout, "ru")
    
    kbdcfg.add_additional_layout("Deutsch",  beautiful.de_layout, "de")
    kbdcfg.add_additional_layout("Français", beautiful.fr_layout, "fr")
    kbdcfg.bind()
  3. Bind your mouse keys:

    -- Mouse bindings
    kbdcfg.widget:buttons(
     awful.util.table.join(awful.button({ }, 1, function () kbdcfg.switch_next() end),
                           awful.button({ }, 3, function () kbdcfg.menu:toggle() end))
    )
  4. Bind your keyboard shortcuts:

    globalkeys = awful.util.table.join(globalkeys,
        -- Shift-Alt to change keyboard layout
        awful.key({"Shift"}, "Alt_L", function () kbdcfg.switch_next() end),
        -- Alt-Shift to change keyboard layout
        awful.key({"Mod1"}, "Shift_L", function () kbdcfg.switch_next() end)
    )
  5. Add widget to your wibar:

    s.mywibox = awful.wibar({ position = "top", screen = s })
    
    -- Add widgets to the wibox
    s.mywibox:setup {
        layout = wibox.layout.align.horizontal,
        { -- Left widgets
            -- ...
        },
        { -- Middle widgets
            -- ...
        },
        { -- Right widgets
            -- ...
            kbdcfg.widget,
            -- ...
        },
    }

Options

The following options you can pass to create function:

  • type - set type of widget. It is two types of widget gui or tui. Default: tui
  • cmd - command to change layout. For more information see section: How to use a different layout switch command. Default: setxkbmap
  • default_layout_index - set index of default layout. Default: 1
  • remember_layout - to remember selected layout per window set it to true. Default: false
  • tui_wrap_left, tui_wrap_right - allows you to customise the wrapping around the tui version of the widget. Default: " "

Functions

  • switch_next() - this function switches one primary keyboard layout to the next primary layout.
  • bind() - this function applies all settings to the widget.
  • switch_by_name(name) - this function is mostly used for setting additional layouts. It gets a layout name (first parameter of add_xxxx_layout() of layout that should be set.
  • add_primary_layout(name, label, subcmd) - this function adds a primary layout to the widget.
  • add_additional_layout(name, label, subcmd) - this function adds additional layout to the widget.

How to use a different layout switch command

By default the widget uses setxkbmap command to switch keyboard layouts. The following parameters allow it to use a non-standard layout switcher, for example the Fcitx for Chinese/Japanese/Korean input:

local kbdcfg = keyboard_layout.kbdcfg({cmd = "fcitx-remote -s", type = "tui"})

kbdcfg.add_primary_layout("English",  "us", "fcitx-keyboard-us")
kbdcfg.add_primary_layout("Russian",  "ru", "fcitx-keyboard-ru-ruu")
kbdcfg.add_primary_layout("Japanese", "ja", "mozc")

Note, that you should pass a valid input method name to fcitx-remote command. The last layout in the example uses the Mozc as an input method for Japanese input.

TUI wrap configuration

By default, the tui version adds a " " wrapping around the widget as a margin. In certain cases however, this might not be what you want. To change the wrapping around the widget, modify your config as such:

local kbdcfg = keyboard_layout.kbdcfg({
    type = "tui",
    tui_wrap_right = "",  -- wrapping on the right
    tui_wrap_left = ""  -- wrapping on the left
})

The example given should result in something kinda like this:

Screenshot of the widget accompanied by a nerd font keyboard icon on the left

Screenshots

In the beginning of both screen casts I changed layouts by keyboard shortcats and only primary layouts were switched. After that I showed how additional layouts work.

Usage of gui_layout: Usage of gui_layout.gif

Usage of tui_layout Usage of tui_layout.gif

References

Author

Egor Churaev [email protected]

Licence

MIT

Flags icons

Icon with British flag I take from here: https://www.gosquared.com/resources/flag-icons/

keyboard_layout's People

Contributors

adsoncicilioti avatar asavonic avatar echuraev avatar krydos avatar naafx8 avatar octelly avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

keyboard_layout's Issues

Caps lock

How I can map CAPS LOCK switching language?

Problems with layouts with special characters

os.execute(kbdcfg.cmd .. " " .. layout.subcmd)

This line caused problems for me. The reason is that, I use the following config:

kbdcfg.add_primary_layout("English (US)", "EN", "us")
kbdcfg.add_primary_layout("Czech (QWERTY)", "CZ", "cz(qwerty)")

Since the layout's name doesn't get wrapped in quotes, the cz(qwerty) layout causes problems. I fixed it by modifying my local copy as such:

os.execute(kbdcfg.cmd .. " \"" .. layout.subcmd .. "\"")

Init.lua don't allow put/clone on another directory

Many users like to put their modules, widgets, plugins, etc, in another folder to be more organized, instead of putting them in the root of the awesome config directory.

For example:
I have the modules folder where I put all the third party libs and modules. When requesting this lib, I just call it like this:
local kblayout = require("modules.keyboard_layout")
or
local kblayout = require("modules/keyboard_layout")

As your init.lua is, the call is absolute, not allowing to use in a custom directory to improve the organization.

kbdcfg not found

Awesome cannot find a command kbdcfg. Which package do i need to install?

Per-layout command / more flexible commands

My use case is that I alternate between US and US Dvorak. For both, I have a setxkbmap option and I then reload ~/.xmodmaprc. I use this rather unfortunate line:

kbdcfg.add_primary_layout("Dvorak", "DV", "-option\" compose:ralt  dvorak && xmodmap ~/.xmodmaprc && echo \"")

This is to "hack" into the os.execute that is done by the code.

A much more elegant solution would be to have a add_primary_layout with named arguments name, icon, full_command, cmd_argument, with the last two optional, although one of the two should be there. For instance:

kbdcfg.add_primary_layout_opt { name = "Dvorak", icon = "DV",
   full_command = "setxkbmap -option compase:ralt dvorak && xmodmap ~/.xmodmaprc" }

xbdcfg.add_primary_layout_opt { name = "US", icon = "US", cmd_argument = "us" }

Alternatively, we could specify the command when creating kbdcfg as:

cmd = "setxkbmap -option compose:ralt \"{}\" && xmodap ~/.xmodmaprc"

… but this does not provide the same flexibility.

US-INTL-ALTGR

how would you add the the variants (US-INTL-ALTGR, in my case) in this? i'm quite confused how to write them at the .add_primary_layout part

┌─(^.^)-(~)
└─> setxkbmap -print -verbose 10
Setting verbose level to 10
locale is C
Trying to load rules file ./rules/evdev...
Trying to load rules file /usr/share/X11/xkb/rules/evdev...
Success.
Applied rules from evdev:
rules:      evdev
model:      pc105
layout:     us,us
variant:    altgr-intl,
Trying to build keymap using the following components:
keycodes:   evdev+aliases(qwerty)
types:      complete
compat:     complete
symbols:    pc+us(altgr-intl)+us:2+inet(evdev)
geometry:   pc(pc105)
xkb_keymap {
	xkb_keycodes  { include "evdev+aliases(qwerty)"	};
	xkb_types     { include "complete"	};
	xkb_compat    { include "complete"	};
	xkb_symbols   { include "pc+us(altgr-intl)+us:2+inet(evdev)"	};
	xkb_geometry  { include "pc(pc105)"	};
};

Remember input method per winodw

Hey @echuraev
Thanks for the package.

Don't you know if there is any way to remember what keyboard layout was selected previously for a window?

Like Chrome has EN and Skype has RU.
When I switch from Skype (RU) to Chrome (EN) the input method is also change. It usually called Allow different layouts for individual windows

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.