Author Topic: How are Shifted characters implemented in software?  (Read 1885 times)

0 Members and 1 Guest are viewing this topic.

Offline wolfv

  • Thread Starter
  • Posts: 269
How are Shifted characters implemented in software?
« on: Tue, 17 December 2013, 00:04:20 »
Normally Shift characters like ~!@#$%^&()_{}<> are activated with the shift key.
Some keyboard software can activate Shift characters via layer key e.g. ErgoDox.

I am writing a keyboard program for Teensy that activates Shift characters via layer key.
Does anyone know how Shifted characters are implemented in ErgoDox or any keyboard software?
I would greatly appreciate it if you could point me to an open source file where Shifted characters are implemented.

Thank you.

Offline sth

  • 2 girls 1 cuprubber
  • Posts: 3438
Re: How are Shifted characters implemented in software?
« Reply #1 on: Tue, 17 December 2013, 00:30:30 »
i am not a firmware dev so i may not be the best one to explain this, but from my understanding, the shifted characters are implemented at the OS level. you might be thinking of the function layers, which are implemented at the keyboard firmware level.
11:48 -!- SmallFry [~SmallFry@unaffiliated/smallfry] has quit [Ping timeout: 245 seconds] ... rest in peace

Offline nuclearsandwich

  • Posts: 752
  • Location: Santa Clara Valley, CA
Re: How are Shifted characters implemented in software?
« Reply #2 on: Tue, 17 December 2013, 01:01:27 »
You could check out the ergodox firmware. https://github.com/benblazak/ergodox-firmware

Offline wolfv

  • Thread Starter
  • Posts: 269
Re: How are Shifted characters implemented in software?
« Reply #3 on: Tue, 17 December 2013, 01:02:12 »
Hi sth,

That's true.  Somehow the keyboard key is sending a shift code + key code to the OS.

The following lines are coped from arduino-1.0.5\hardware\teensy\cores\teensy\keylayouts.c:
Code: [Select]
#define SHIFT_MASK 0x0040
...
#define ASCII_2D KEY_MINUS // 45 -
...
#define ASCII_5F KEY_MINUS + SHIFT_MASK // 95 _
For example, there is probably a way to use the ASCII_5F macro to implement the underscore character, but I don't see how to do it.

Offline wolfv

  • Thread Starter
  • Posts: 269
Re: How are Shifted characters implemented in software?
« Reply #4 on: Tue, 17 December 2013, 01:09:17 »
You could check out the ergodox firmware. https://github.com/benblazak/ergodox-firmware

Hi nuclearsandwich,

I briefly looked at the ergodox firmware and released figuring out where the Shift characters are implemented will take much time.  I am hoping that someone familiar with the firmware can point me to the relevant file.

Offline Neebio

  • Posts: 115
  • Location: Canada
  • Dance Master
Re: How are Shifted characters implemented in software?
« Reply #5 on: Tue, 17 December 2013, 01:14:22 »
Hi wolfv,
I'm going to assume that the keyboard handles this, and without thinking too hard about the best way it would do it, I'll just throw out some human-readable pseudo code:

Code: [Select]
// (inside some kind of input checking loop or function)
if function_layer_key then
...
if key_you_press then
send( shift_key_press)
send( whatever_key_press)
end
...
end

Basically, my feeling is that while the keyboard (teensy in your case) is scanning the matrix to see what is being pressed, it does a compare against the function layer key, and if that is pressed, it just sends a different (or combination of) key press signal(s) to the computer.
RK9000RE w/ Raindrop & DDR arrow keys
Cherry G80-11900LUMEU-2 w/ Red/Black SA keys

Offline wolfv

  • Thread Starter
  • Posts: 269
Re: How are Shifted characters implemented in software?
« Reply #6 on: Tue, 17 December 2013, 02:41:16 »
Hi Neebio,

I was thinking the same thing.  Here is the preliminary code:
Code: [Select]
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

//macros copied from arduino-1.0.5\hardware\teensy\cores\teensy\keylayouts.c
#define MODIFIERKEY_SHIFT       ( 0x02 | 0x8000 )
#define KEY_MINUS         ( 45  | 0x4000 )
#define SHIFT_MASK 0x0040
#define ASCII_5F KEY_MINUS + SHIFT_MASK // 95 _

int main()
{
int key = ASCII_5F; // the underscore key from layout

if (key & SHIFT_MASK == SHIFT_MASK)
{
cout << "send(MODIFIERKEY_SHIFT)" << endl;
cout << "send(" << key - SHIFT_MASK << ")" << endl;
cout << "         " << KEY_MINUS; // should be equal to key - SHIFT_MASK
}
}
output:
Code: [Select]
send(MODIFIERKEY_SHIFT)
send(16429)
         16429
« Last Edit: Tue, 17 December 2013, 08:48:36 by wolfv »

Offline suka

  • Posts: 52
Re: How are Shifted characters implemented in software?
« Reply #7 on: Tue, 17 December 2013, 05:34:21 »
i am not a firmware dev so i may not be the best one to explain this, but from my understanding, the shifted characters are implemented at the OS level.
Even worse, the selected keyboard layout in the OS has to be also considered, as for example some keycodes and symbols on the shift/altgr "layers" are different.  That's why i chose to store each modifier necessary together with each character to be able to switch languages and also put keys needing different modifiers on one layer in my Keyboard firmware. Essentially, this conpletely decouples the keycode matrix from any layer modifiers, which I think neither tmk nor the Ergodox fw caters for. But with macros you could work around that limitation, too

Offline wolfv

  • Thread Starter
  • Posts: 269
Re: How are Shifted characters implemented in software?
« Reply #8 on: Tue, 17 December 2013, 09:44:48 »
store each modifier necessary together with each character to be able to switch languages and also put keys needing different modifiers on one layer in my Keyboard firmware. Essentially, this completely decouples the keycode matrix from any layer modifiers

Thanks suka.
After poking around the USB standards I came to the same conclusion.  What a mess.  It would have been nice if only letters were coupled to shift.
I am in the process of writing a wrapper class called "ScanCodes" that decouples keycodes from keyboard position.
« Last Edit: Tue, 17 December 2013, 09:47:19 by wolfv »