Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions src/Keyboard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/**
* We use an abstract class for Keyboard for future possibilities of different keyboard layouts
* that a user wants to script input for
*/
public class Keyboard {

/**
* The keyboard mapping to use for scripting the lines from input files
*/
private char[][] keyboardLayout;

/**
* Constructor for this class, sets value of keyboard
* @param keyboardLayout - the keyboard mapping to be used for this instance of Keyboard
*/
public Keyboard(char[][] keyboardLayout) {
this.keyboardLayout = keyboardLayout;
}

/**
* Takes the input and determines the script necessary to obtain that input
* @param input - a String to convert into a script
* @return a String with the script representing how to obtain the input for this particular keyboard
*/
String scriptLine(String input) {
// Initializes the builder string and converts input to uppercase to match the keyboard layouts
String result = "";
String uppercase = input.toUpperCase();

// The Point objects we will use to store locations and calculate the paths
Point initial = new Point();
Point destination = new Point();

// Iterate through each character in the string, performing the correct conversions on each
for(int i = 0; i < uppercase.length(); i++) {
char letter = uppercase.charAt(i);

if(letter == ' ')
// A space
result += 'S';
else if(keyboardContains(letter)) {
// A character inside the keyboard
result += scriptLetter(letter, initial, destination);
} else {
// Not a character inside the keyboard - error
result += 'E';
}

if(i < uppercase.length() - 1)
result += ",";
}

return result;
}

/**
* Takes letterToScript and converts it to a sequence of U, D, L and R's, ending with a #
* @param letterToScript - the letter to convert
* @return a string formatted as explained above
*/
private String scriptLetter(char letterToScript, Point initialPoint, Point destinationPoint) {
String script = "";

// Sets the correct location of the destination of the letter we are now searching for
setDestinationPoint(letterToScript, destinationPoint);

// Calculate the displacement (the path)
int verticalDisplacement = destinationPoint.getI() - initialPoint.getI();
int horizontalDisplacement = destinationPoint.getJ() - initialPoint.getJ();

// Based on value of displacement, add appropriate letters to the string we are building
if(verticalDisplacement < 0) {
verticalDisplacement *= -1;
script += addLetters(verticalDisplacement, 'U');
} else {
script += addLetters(verticalDisplacement, 'D');
}

if(horizontalDisplacement < 0) {
horizontalDisplacement *= -1;
script += addLetters(horizontalDisplacement, 'L');
} else {
script += addLetters(horizontalDisplacement, 'R');
}

script += "#";

// Current destination point becomes initial point for the next letter (represents the cursor)
initialPoint.setI(destinationPoint.getI());
initialPoint.setJ(destinationPoint.getJ());

return script;
}

/**
* Builds a string consisting of toAdd repeated numTimes times with commas in following each time
* @param numTimes - the number of times to add the character
* @param toAdd - the character to add to the string we are building
* @return a String formatted as explained above
*/
private String addLetters(int numTimes, char toAdd) {
String builder = "";

for(int i = 0; i < numTimes; i++)
builder += toAdd + ",";

return builder;
}

/**
* Searches for letterToFind in keyboardLayout and sets the location of it in pointToSet
* @param letterToFind - the letter whose location we are searching for
* @param pointToSet - the Point whose location we will set to match letterToFind, represents the destination
*/
private void setDestinationPoint(char letterToFind, Point pointToSet) {
for(int i = 0; i < keyboardLayout.length; i++) {
for(int j = 0; j < keyboardLayout[0].length; j++) {
if(letterToFind == keyboardLayout[i][j]) {
pointToSet.setI(i);
pointToSet.setJ(j);
}
}
}
}

/**
* Checks if keyboardLayout contains letterToCheck
* @param letterToCheck - the letter desired in the keyboard
* @return true if keyboardLayout contains letterToCheck, false otherwise
*/
private boolean keyboardContains(char letterToCheck) {
for(char[] row : keyboardLayout) {
for(char c : row) {
if(c == letterToCheck)
return true;
}
}

return false;
}
}
137 changes: 137 additions & 0 deletions src/KeyboardScripter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;

/**
* The driver class for this program, takes file input, reads it
* and outputs scripted version of each line
*/
public class KeyboardScripter {

public static void main(String[] args) {

// Strings to be used to access files
String rootPath = "/Users/michaelbarton/IdeaProjects/OnScreenKeyboard/src/";
String keyboardLayoutsPath = "keyboardLayouts/";
String inputFilesPath = "inputFiles/";
String alphabeticalFileName = "alphabeticalLayout.txt";
String qwertyFileName = "qwertyLayout.txt";

// Other attributes needed for main
Scanner sc = new Scanner(System.in);
String keyboardType, scriptFileName;
char[][] keyboardLayout;
Keyboard keyboard;

// Print welcome message and determine keyboard type
System.out.println("Welcome to KeyboardScripter!");

do {
System.out.print("Enter which kind of keyboard you'd like to use (\'alphabetical\' or \'qwerty\'): ");
keyboardType = sc.nextLine();
} while (!keyboardType.equals("alphabetical") && !keyboardType.equals("qwerty"));

// Create Keyboard based on keyboard type inputted from user
if(keyboardType.equals("alphabetical")) {
keyboardLayout = createKeyboardLayout(rootPath + keyboardLayoutsPath + alphabeticalFileName);
} else {
keyboardLayout = createKeyboardLayout(rootPath + keyboardLayoutsPath + qwertyFileName);
}

keyboard = new Keyboard(keyboardLayout);

System.out.println("\nHere's what your keyboard looks like!");
print2DArray(keyboardLayout);

// Obtain file name to read through and script
System.out.print("\nEnter the file name that you would like to script: ");
scriptFileName = sc.nextLine();

System.out.println("\nResults:\n");

// Open file, read through and script each line
try {
BufferedReader br = new BufferedReader(new FileReader(new File(rootPath + inputFilesPath + scriptFileName)));

String str;
while ((str = br.readLine()) != null) {
System.out.println("\"" + str + "\" scripts to: ");
System.out.println(keyboard.scriptLine(str) + "\n");
}

} catch (Exception e) {
e.printStackTrace();
}

}

/**
* Reads in the keyboard layout from the file and converts it into a double array of characters
* @param fileName - name of the file to read from
* @return double array of characters
*/
static char[][] createKeyboardLayout(String fileName) {

// Must use a list as we don't know the size yet
ArrayList<ArrayList<Character>> table = new ArrayList<>();

// Read the lines into "table"
try {
BufferedReader br = new BufferedReader(new FileReader(new File(fileName)));

String str;
while ((str = br.readLine()) != null)
table.add(parseCharacters(str));

} catch (Exception e) {
e.printStackTrace();
}

// Create the double char array based on dimensions of "table"
int rows = table.size();
int columns = table.get(0).size();
char[][] layout = new char[rows][columns];

// Extract out all characters from table into the double char array "layout"
for(int i = 0; i < rows; i++) {
for(int j = 0; j < columns; j++) {
layout[i][j] = table.get(i).get(j);
}
}

return layout;
}

/**
* Splits up a line of characters into a list based on a space regex
* @param str - the line of characters to split up, each token should be a string of length 1
* @return an ArrayList of characters representing a split version of str
*/
private static ArrayList<Character> parseCharacters(String str) {
ArrayList<Character> characters = new ArrayList<>();

String[] splitString = str.split(" ");

for(String s : splitString) {
characters.add(s.charAt(0));
}

return characters;
}

/**
* Prints out any char[][] array
* @param array - the double array to print out
*/
private static void print2DArray(char[][] array) {
for(char[] row : array) {
for (char element : row) {
System.out.print(element + " ");
}
System.out.println();
}
}

}
Loading