Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <stdbool.h>
- typedef struct
- {
- char* keys;
- } Keyboard;
- Keyboard* createKeyboard(int);
- bool getKeyboardKey(Keyboard* , int);
- void setKeyboardKey(Keyboard* , int, bool);
- int main()
- {
- Keyboard* mykeys = createKeyboard(20);
- setKeyboardKey(mykeys, 5, 1);
- getKeyboardKey(mykeys, 5);
- }
- Keyboard* createKeyboard(int ksize)
- {
- ksize = ksize < 8 ? 8 : ksize;
- Keyboard* kb;
- kb = malloc(sizeof(Keyboard));
- kb->keys = malloc(sizeof(char)*ceil(ksize/8));
- return kb;
- }
- bool getKeyboardKey(Keyboard* kb, int key)
- {
- char* keyptr = kb->keys;
- int keybyte = floor(key/8);
- int keybit = (key%8);
- keyptr += (keybyte-1);
- char kbyte = *keyptr;
- bool kbit = (kbyte >> keybit) & 0x01;
- printf("Bits: %i",kbit);
- return kbit;
- }
- void setKeyboardKey(Keyboard* kb, int key, bool onoff)
- {
- char* keyptr = kb->keys;
- int keybyte = floor(key/8);
- int keybit = (key%8);
- if(onoff)
- {
- kb->keys[keybyte-1] |= (1 << keybit);
- }
- else
- {
- kb->keys[keybyte-1] &= ~(1 << keybit);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement