Skip to content

Instantly share code, notes, and snippets.

@Pinacolada64
Created April 29, 2023 05:58
Show Gist options
  • Save Pinacolada64/6e2444dffd60c1adf5ba3a5b4ef6b7d6 to your computer and use it in GitHub Desktop.
Save Pinacolada64/6e2444dffd60c1adf5ba3a5b4ef6b7d6 to your computer and use it in GitHub Desktop.
Demonstrate a Commodore keymap with named keys.
import sys
def show_keymap():
for value, key_name in keymap.items():
print(f"{value:3} {key_name}")
if value % 20 == 0:
_ = input("Pause ('Q' quits): ")
if _.lower() == "q":
print("Aborted.")
break
if __name__ == '__main__':
key_nums = [x for x in range(0, 256)]
# list comprehension makes a list within a list, so that's out
# start out with '<null>' in list index 0
key_names = ["Null"]
# iterate through ['Ctrl-A', 'Ctrl-B' ... 'Ctrl-Y', 'Ctrl-Z']
for x in range(ord("A"), ord("Z") + 1):
key_names.append(f"Ctrl-{chr(x)}")
# iterate through [27 ... 255]
for x in range(27, 255 + 1):
key_names.append(f'{chr(x)}')
keymap = {value: name for value, name in zip(key_nums, key_names)}
print("Before key name replacement:")
show_keymap()
# sys.exit()
# override generic "Ctrl-<letter>" keys with more meaningful names:
ctrl_key_nice_names = {0: "Null", 3: "Stop", 5: "White", 8: "Disable Shift + C=",
9: "Enable Shift + C=", 13: "Return", 14: "Lowercase",
17: "Cursor Down", 18: "Reverse On", 19: "Home", 20: "Delete",
28: "Red", 29: "Cursor Right", 30: "Green", 31: "Blue",
129: "Orange",
133: "f1", 134: "f3", 135: "f5", 136: "f7",
137: "f2", 138: "f4", 139: "f6", 140: "f8",
141: "Shift + Return", 142: "Uppercase",
144: "Black", 145: "Cursor Up", 147: "Clear", 148: "Insert",
149: "Brown", 150: "Lt. Red", 151: "Gray 1", 152: "Gray 2",
153: "Lt. Green", 154: "Lt. Blue", 155: "Gray 3",
156: "Purple", 157: "Cursor Left", 158: "Yellow",
159: "Cyan", 160: "Shift + Space"}
# assign descriptive name from ctrl_key_nice_names dict:
# TODO: this table is also used when doing a .Read to show nice names:
# e.g., instead of '\x95', show '[Brown]'
keymap.update(ctrl_key_nice_names)
print("After key name replacement:")
show_keymap()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment