Burathar
5 years ago
7 changed files with 258 additions and 0 deletions
@ -0,0 +1,13 @@
@@ -0,0 +1,13 @@
|
||||
#! /bin/python3 |
||||
|
||||
from openrazer_command import Keyboard, Color |
||||
|
||||
keyboard = Keyboard() |
||||
|
||||
if not keyboard.found_keyboard(): |
||||
print("No keyboard was found") |
||||
exit() |
||||
|
||||
keyboard.set_static(Color(255,255,255)) |
||||
keyboard.set_keys(Color.from_hex('ff0000'), 'enter') |
||||
keyboard.draw() |
@ -0,0 +1,2 @@
@@ -0,0 +1,2 @@
|
||||
from .keyboard import Keyboard |
||||
from .color import Color |
@ -0,0 +1,48 @@
@@ -0,0 +1,48 @@
|
||||
class Color: |
||||
|
||||
def __init__(self, red, green, blue): |
||||
if not (Color.check_color_value(red) or Color.check_color_value(green) or Color.check_color_value(blue)): |
||||
print(f"Color is out of range") |
||||
return |
||||
self.red = red |
||||
self.green = green |
||||
self.blue = blue |
||||
|
||||
@classmethod |
||||
def from_hex(cls, hexstring): |
||||
"Initialize Color from hexstring (e.g. #ffffff)" |
||||
if isinstance(hexstring, str): |
||||
if hexstring[0] == '#': |
||||
hexstring = hexstring[1:] |
||||
if len(hexstring) == 6: |
||||
color = tuple(int(hexstring[i:i+2], 16) for i in (0, 2, 4)) |
||||
return cls(color[0], color[1], color[2]) |
||||
|
||||
@classmethod |
||||
def from_name(cls, color_name): |
||||
"Initialize Color from name (e.g. 'white')" |
||||
color_name = str.lower(color_name) |
||||
|
||||
if color_name == 'white': |
||||
return cls(255,255,255) |
||||
elif color_name == 'black': |
||||
return cls(0, 0, 0) |
||||
elif color_name == 'red': |
||||
return cls(255, 0, 0) |
||||
elif color_name == 'green': |
||||
return cls(0, 255, 0) |
||||
elif color_name == 'blue': |
||||
return cls(0, 0, 255) |
||||
else: |
||||
raise ValueError(f'Colorname {color_name} was not recognised') |
||||
|
||||
|
||||
@staticmethod |
||||
def check_color_value(value): |
||||
if not isinstance(value, int): |
||||
print(f"Colorvalue '{value}' is not a number") |
||||
return False |
||||
if value < 0 or value > 255: |
||||
print(f"Color value '{value}' is out of bounds (0-255)") |
||||
return False |
||||
return True |
@ -0,0 +1,49 @@
@@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env python3 |
||||
|
||||
from .keyboard import Keyboard |
||||
from .keymap import Keymap |
||||
from .color import Color |
||||
import openrazer.client |
||||
|
||||
from pynput import keyboard as keyboardnput |
||||
|
||||
keyboard = Keyboard(no_keymap=True) |
||||
|
||||
if not keyboard.found_keyboard(): |
||||
print("No keyboard was found") |
||||
exit() |
||||
|
||||
last_key_pressed = None |
||||
def on_press(key): |
||||
global last_key_pressed |
||||
last_key_pressed = key |
||||
return False |
||||
|
||||
def get_key(): |
||||
with keyboardnput.Listener(on_press=on_press) as listener: |
||||
listener.join() |
||||
|
||||
def set_key_columns(keymap, column_range, prefix=""): |
||||
for row in range(0, 6): |
||||
for column in column_range: |
||||
keyboard.clear() |
||||
keyboard.set_key(row,column, Color(255, 255, 255)) |
||||
keyboard.draw() |
||||
get_key() |
||||
print('{} was pressed'.format(last_key_pressed)) |
||||
keymap[prefix + str(last_key_pressed)] = row * 22 + column |
||||
|
||||
def iter_keys(): |
||||
keymap = dict() |
||||
print("Make sure caps lock and numpad are disabled, if not, do it now and restart the script") |
||||
print("This script will iterate over all keys, row by row. When a key lights up white, press it, and use the mouse to cancel any F-key effects. First, we'll skip the numpad. If no key lights up, press the bottom-right most key, excluding the numpad(right-arrow).") |
||||
set_key_columns(keymap, range(0, 18)) |
||||
print("Now we'll iterate over the numpad, if no key lights up, press the bottom right key(enter)") |
||||
set_key_columns(keymap, range(18, 22), "num_") |
||||
return keymap |
||||
|
||||
|
||||
keymap = Keymap() |
||||
keymap.set_keys(iter_keys()) |
||||
keymap.write_keymap_file(keyboard.keyboard_name) |
||||
print('Saved keymap to file (.config/openrazer_scripter/keymap.json)') |
@ -0,0 +1,73 @@
@@ -0,0 +1,73 @@
|
||||
import warnings |
||||
from collections.abc import Iterable |
||||
|
||||
import openrazer.client |
||||
|
||||
from .color import Color |
||||
from .keymap import Keymap |
||||
|
||||
|
||||
class Keyboard: |
||||
|
||||
"""Provides tooling to call openrazer.client.device""" |
||||
|
||||
def __init__(self, no_keymap = False): |
||||
self.kbd = None |
||||
self.find_keyboard(no_keymap) |
||||
if self.kbd is None: |
||||
warnings.warn('Compatible keyboard was not detected', RuntimeWarning) |
||||
|
||||
def find_keyboard(self, no_keymap): |
||||
if self.kbd is not None: |
||||
print('keyboard was already initiated') |
||||
return True |
||||
|
||||
devman = openrazer.client.DeviceManager() |
||||
|
||||
for device in devman.devices: |
||||
if (device.name == "Razer BlackWidow Chroma V2"): |
||||
self.kbd = device |
||||
if not no_keymap: |
||||
self.keymap = Keymap() |
||||
self.keymap.load_from_file(self.kbd.name) |
||||
return True |
||||
return False |
||||
|
||||
def found_keyboard(self): |
||||
return self.kbd is not None |
||||
|
||||
def keyboard_name(self): |
||||
return self.kbd.name |
||||
|
||||
def set_keys(self, color, *keys): |
||||
if not isinstance(color, Color): |
||||
print(f"Color {color} for key(s) '{keys}' is no instance of Color, skipping.") |
||||
return False |
||||
for key in keys: |
||||
if isinstance(key, Iterable) and not isinstance(key, str): # Allow for iterables (lists etc) of keys to be parsed |
||||
for single_key in key: |
||||
self.set_keys(color, single_key) |
||||
else: |
||||
location = self.keymap.get_location(key) |
||||
self.kbd.fx.advanced.matrix[location] = [color.red, color.green, color.blue] |
||||
return True |
||||
|
||||
def set_key(self, row, column, color): |
||||
if not isinstance(color, Color): |
||||
print(f"Color '{color}' for key {row},{column} is no instance of Color, skipping.") |
||||
return False |
||||
self.kbd.fx.advanced.matrix[row, column] = [color.red, color.green, color.blue] |
||||
|
||||
def set_static(self, color): |
||||
if not isinstance(color, Color): |
||||
print(f"Color for set_static() is no instance of Color, skipping.") |
||||
return False |
||||
for row in range(0,6): |
||||
for column in range(0, 22): |
||||
self.kbd.fx.advanced.matrix[row,column] = [color.red, color.green, color.blue] |
||||
|
||||
def clear(self): |
||||
self.set_static(Color(0, 0, 0)) |
||||
|
||||
def draw(self): |
||||
self.kbd.fx.advanced.draw() |
@ -0,0 +1,43 @@
@@ -0,0 +1,43 @@
|
||||
from pathlib import Path |
||||
import json |
||||
import math |
||||
|
||||
class Keymap: |
||||
keymap_dir = Path.home() / '.config/openrazer_scripter/' |
||||
def __init__(self): |
||||
self.keys = dict() |
||||
pass |
||||
|
||||
@classmethod |
||||
def get_keymap_path(cls, keyboard_name): |
||||
return cls.keymap_dir / f"keymap_{keyboard_name.replace(' ', '_')}.json" |
||||
|
||||
def load_from_file(self, keyboard_name): |
||||
path = self.get_keymap_path(keyboard_name) |
||||
try: |
||||
with open(path,'r') as keymap_file: |
||||
self.keys = json.load(keymap_file) |
||||
except FileNotFoundError: |
||||
print(f"No such file: '{path}'. Run 'create_keyboard_map.py' to generate a keymap, or move an existing one to the directory.") |
||||
return False |
||||
return True |
||||
|
||||
def write_keymap_file(self, keyboard_name): |
||||
keymap_json = json.dumps(self.keys) |
||||
path = self.get_keymap_path(keyboard_name) |
||||
Path(path.parent).mkdir(parents=True, exist_ok=True) |
||||
with open(path,'w') as keymap_file: |
||||
keymap_file.write(keymap_json) |
||||
|
||||
def set_keys(self, keys): |
||||
self.keys = keys |
||||
|
||||
def get_row_column(self, index): |
||||
row = math.floor(index / 22) |
||||
column = index % 22 |
||||
return (row, column) |
||||
|
||||
def get_location(self, key): |
||||
if key in self.keys.keys(): |
||||
value = self.keys[key] |
||||
return self.get_row_column(value) |
@ -0,0 +1,30 @@
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env python3 |
||||
|
||||
from .keyboard import Keyboard |
||||
from .color import Color |
||||
from .keymap import Keymap |
||||
|
||||
keyboard = Keyboard(no_keymap=False) |
||||
if not keyboard.found_keyboard(): |
||||
print("No keyboard was found") |
||||
exit() |
||||
|
||||
def sort_dict(dictionary): |
||||
return {k: v for k, v in sorted(dictionary.items(), key=lambda item: item[1])} |
||||
|
||||
def iter_keys(keymap): |
||||
sorted_keys = sort_dict(keymap.keys) |
||||
for key in sorted_keys.keys(): |
||||
keyboard.clear() |
||||
print(f'key: {key}') |
||||
keyboard.set_keys(Color(255, 255, 255), key) |
||||
keyboard.draw() |
||||
input() |
||||
|
||||
keymap = keyboard.keymap |
||||
|
||||
while True: |
||||
keyboard.set_keys(Color(255, 255, 255), list(keymap.keys.keys())) |
||||
keyboard.draw() |
||||
input("Press Enter to continue...") |
||||
iter_keys(keymap) |
Loading…
Reference in new issue