commit 9423701450832065996daa69bef4fd0b000961b5 Author: 61616 Date: Fri May 13 16:19:46 2022 -0400 Initial commit diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/KeyPressesBasic.pde b/KeyPressesBasic.pde new file mode 100644 index 0000000..4329037 --- /dev/null +++ b/KeyPressesBasic.pde @@ -0,0 +1,60 @@ +/* + B12NumbersV3 + Basic processing key press tracker for reference. + by Nayan Sawyer + started May13 2022 + version 1.0.0 May 13 2022 +*/ + +int[] keys; // Declare key tracking array + +void setup(){ + keys = new int[0]; // Initialize key tracking array +} + +void draw(){ + + // Output for demostration and debugging + for(int i = 0; i < keys.length; i++){ + print(keys[i]); + } + println(); +} + +void keyPressed(){ + /* + Add key to keys[] when it is pressed, but only add it one time + */ + if(key == CODED){ + if(findInt(keys,keyCode) == -1){ // If key is not in keys[] + keys = (int[])append(keys,keyCode); // Add keyCode to keys[] + } + } + else{ + if(findInt(keys,key) == -1){ // If key is not in keys [] + keys = (int[])append(keys,key); // Add key to keys[] + } + } +} + +void keyReleased(){ + /* + Remove key from keys when it is released + */ + int index = key == CODED ? findInt(keys,keyCode) : findInt(keys,key); // Get index of released key taking into account CODED status + if(index != -1){ // If key is in keys[] + keys[index] = keys[keys.length - 1]; // Swap key at end into the index to be replaced + keys = (int[])shorten(keys); // Remove last key entry + } +} + +int findInt(int[] arr, int _in){ + /* + Returns index of first instance of a given int in an array, and -1 if it is not present + */ + if(arr.length == 0){return -1;} // Return -1 if array is empty + for(int i = 0; i < arr.length; i++){ // For each element in the array + if(arr[i] == _in){return i;} // If array[i] is the desired number return i + } + return -1; // If the whole array has been parsed without returning return -1 +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..7722ea1 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# KeyPressesBasic + Basic Processing key press tracker for reference