Arduino UNO Klavye Olarak Kullanma
Arduino UNO Klavye Olarak Kullanma
İlginç bir Arduino projesi ile karşınızdayım aslında bu projenin ilginç olmasının sebebi normalde bilgisayar ile etkileşimli projelerde Arduino Leonardo kullanılması ama bizim bu projede Arduino Uno kullanmamız ve yazdığımız bir emulatör yardımı sayesinde bilgisayar ile etkileşim kurmasını sağlayacak olmamız.
Ben bu uygulamada sonunda örnek olarak RFID kartını okutarak Instagram hesabıma giriş yaptırdım, tabi bu çok güvenli olmadı çünkü birisinin eline geçmesi durumunda not defterine aynı işlemi uygulayıp şifremi görebilirdi ama sizler en azından klavyeden nasıl bir şeyler yazdırabileceğinizi bu proje ile anlayabilirsiniz.
Not : Bu projede şu an yaşadığım sorunlardan birisini size söylemem gerekir ise bilgisayar dilinin İngilizce olması gerekiyor ve seri haberleşme ile ben şu anda Türkçe karakterleri yollamayı başaramadım. Bu proje veya benzerini yapıp Türkçe karakter yollamayı başarabilen birisi olursa yorumda nasıl yaptığını anlatırsa veya bana özelden mail ile ulaştırsa çok memnun olurum.
İlk olarak ben bu projede sürekli olarak Arduinonun klavyeye müdahale etmemesi için RFID kart modülü kullanacağım ve kart okutulduğu zaman Arduinoda kayıtlı olan kelimeyi Serial mesaj olarak yollayacağım ve sonrasında Processing de hazırladığım yazılım ile mesajı alıp klavyeden yazıyormuş gibi yazdıracağım.
İlk olarak Arduino devremizi kuralım ve kodlarımızı yazmaya başlayalım.
Devre Şemamız
Arduinoda Kullandığımız Kodlar
/* Kütüphaneler */ #include <SPI.h> #include <MFRC522.h> /* Değişkenler */ int rf_reset = 9, rf_ss = 10; // RFID Modül bağladığımız reset ve ss pinleri MFRC522 RFID(rf_ss, rf_reset); // RFID Modülü değişkenimiz byte card_id[4] = {185, 144, 187, 176}; // Tanımladığımız kartın ID si bool card_statue = LOW; // Kart yakındamı değilmi tutulan bool değişken void setup() { Serial.begin(9600); // Seri bağlantımız SPI.begin(); // RFID kart ile iletişim bağlantımız RFID.PCD_Init(); // RFID kart ile iletişim bağlantımız } void RFIDStatue() { if (!card_statue) { // Kart durumunu kontrol ettiriyoruz ve eğer LOW / False ise if (!RFID.PICC_IsNewCardPresent()) { // Yeni kart yakında değil ise return false; // boş döndürüyoruz } else { // Eklenmiş / yakında ise card_statue = true; // kart durumunu high / true yapıyoruz if (RFID.uid.uidByte[0] == card_id[0] && RFID.uid.uidByte[1] == card_id[1] && RFID.uid.uidByte[2] == card_id[2] && RFID.uid.uidByte[3] == card_id[3]) // Kartı doğruluyoruz sendMessage(); // Doğru kart ise mesajı yolla fonksiyonunu çağırıyoruz. } } if (!RFID.PICC_ReadCardSerial()) { // Eğer kart yakında değilse card_statue = false; // kart durumunu low / false yapıyoruz return false; } delay(100); } void sendMessage() { Serial.println("Yollanan Kelime"); // Mesaj yollamak için println i kullanıyoruz } void loop() { RFIDStatue(); }
Processing Kullandığımız Kodlar
import processing.serial.*; // Seri bağlantısı kurmak için kütüphane import java.awt.Robot; // Klavyeye otomatik tıklamak için olan kütüphane import java.awt.event.KeyEvent; // Klavyeyi simüle den kütüphane import java.io.IOException; // Sisteme erişmek için kütüphane
/* Robot Variable */ Robot robot; // Klavyeye otomatik basmak için bir robot tanımlıyoruz /* Serial Variable */ String serialReadString = null; // Serialden gelen mesaj String newserialReadString = "0"; // Serialdan gelen mesaj aynı ise yazdırmamak için kullandığımız değişken ama bu projede kullanmadık char[] serialReadChar; // Serialden gelen mesajı harf harf bölmek için kullandığımız değişken int nl = 10; // bu sabit bu şekilde dursun ellemeyin, bende ne işe yarıyor bilmiyorum String[] serialPorts; // Cihazın bağlı olduğu tüm seri portları görmek için Serial serial; // Serial bağlantısı için olan kütüphane sınıfına erişim
void setup() { surface.setLocation(9999,9999); // Uygulamayı arka planda çalıştırmayı beceremediğim için ekranın sağ el alt köşesine yolluyorum size(0, 0); // Ekran boyutunu genişlik ve yüksekliğini 0,0 olarak tanımlıyorum SerialPortConnect(); // Seri bağlantı için yazdığımız fonksiyonu çağırıyoruz SerialPortList(); // Sistemdeki portları listeliyoruz SerialConnect(serialPorts[0], 9600); // Burada serialPorts[0] arduinonun bağlı olduğu com portu, 9600 ise arduinodaki serialBaudrate değeri RobotCreate(); // robot oluşturuyoruz, klavyeye otomatik tıklaması için } void draw() { SerialRead(); // Serialden gelen mesajları okumak için olan fonksiyonu çağırıyoruz. surface.setLocation(9999, 9999); }
/* Robot */ void RobotCreate() { try { robot = new Robot(); // Robot oluşturuyoruz robot.delay(1000); // 1 Saniye bekletiyoruz } catch(Exception e) { exit(); } } /* Aşağıda oluşturduğumuz robotun klavyedeki tuşları tanımlıyoruz ve gelen tuşa basmasını sağlıyoruz. */ void RobotKeyPress(char character) { try { if (character == 'a') { robot.keyPress(KeyEvent.VK_A); robot.keyRelease(KeyEvent.VK_A); } else if (character == 'b') { robot.keyPress(KeyEvent.VK_B); robot.keyRelease(KeyEvent.VK_B); } else if (character == 'c') { robot.keyPress(KeyEvent.VK_C); robot.keyRelease(KeyEvent.VK_C); } else if (character == 'd') { robot.keyPress(KeyEvent.VK_D); robot.keyRelease(KeyEvent.VK_D); } else if (character == 'e') { robot.keyPress(KeyEvent.VK_E); robot.keyRelease(KeyEvent.VK_E); } else if (character == 'f') { robot.keyPress(KeyEvent.VK_F); robot.keyRelease(KeyEvent.VK_F); } else if (character == 'g') { robot.keyPress(KeyEvent.VK_G); robot.keyRelease(KeyEvent.VK_G); } else if (character == 'h') { robot.keyPress(KeyEvent.VK_H); robot.keyRelease(KeyEvent.VK_H); } else if (character == 'i') { robot.keyPress(KeyEvent.VK_I); robot.keyRelease(KeyEvent.VK_I); } else if (character == 'j') { robot.keyPress(KeyEvent.VK_J); robot.keyRelease(KeyEvent.VK_J); } else if (character == 'k') { robot.keyPress(KeyEvent.VK_K); robot.keyRelease(KeyEvent.VK_K); } else if (character == 'l') { robot.keyPress(KeyEvent.VK_L); robot.keyRelease(KeyEvent.VK_L); } else if (character == 'm') { robot.keyPress(KeyEvent.VK_M); robot.keyRelease(KeyEvent.VK_M); } else if (character == 'n') { robot.keyPress(KeyEvent.VK_N); robot.keyRelease(KeyEvent.VK_N); } else if (character == 'o') { robot.keyPress(KeyEvent.VK_O); robot.keyRelease(KeyEvent.VK_O); } else if (character == 'p') { robot.keyPress(KeyEvent.VK_P); robot.keyRelease(KeyEvent.VK_P); } else if (character == 'q') { robot.keyPress(KeyEvent.VK_Q); robot.keyRelease(KeyEvent.VK_Q); } else if (character == 'r') { robot.keyPress(KeyEvent.VK_R); robot.keyRelease(KeyEvent.VK_R); } else if (character == 's') { robot.keyPress(KeyEvent.VK_S); robot.keyRelease(KeyEvent.VK_S); } else if (character == 't') { robot.keyPress(KeyEvent.VK_T); robot.keyRelease(KeyEvent.VK_T); } else if (character == 'u') { robot.keyPress(KeyEvent.VK_U); robot.keyRelease(KeyEvent.VK_U); } else if (character == 'v') { robot.keyPress(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_V); } else if (character == 'w') { robot.keyPress(KeyEvent.VK_W); robot.keyRelease(KeyEvent.VK_W); } else if (character == 'x') { robot.keyPress(KeyEvent.VK_X); robot.keyRelease(KeyEvent.VK_X); } else if (character == 'y') { robot.keyPress(KeyEvent.VK_Y); robot.keyRelease(KeyEvent.VK_Y); } else if (character == 'z') { robot.keyPress(KeyEvent.VK_Z); robot.keyRelease(KeyEvent.VK_Z); } else if (character == 'A') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_A); robot.keyRelease(KeyEvent.VK_A); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'B') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_B); robot.keyRelease(KeyEvent.VK_B); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'C') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_C); robot.keyRelease(KeyEvent.VK_C); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'D') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_D); robot.keyRelease(KeyEvent.VK_D); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'E') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_E); robot.keyRelease(KeyEvent.VK_E); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'F') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_F); robot.keyRelease(KeyEvent.VK_F); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'G') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_G); robot.keyRelease(KeyEvent.VK_G); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'H') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_H); robot.keyRelease(KeyEvent.VK_H); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'I') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_I); robot.keyRelease(KeyEvent.VK_I); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'J') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_J); robot.keyRelease(KeyEvent.VK_J); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'K') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_K); robot.keyRelease(KeyEvent.VK_K); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'L') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_L); robot.keyRelease(KeyEvent.VK_L); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'M') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_M); robot.keyRelease(KeyEvent.VK_M); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'N') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_N); robot.keyRelease(KeyEvent.VK_N); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'O') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_O); robot.keyRelease(KeyEvent.VK_O); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'P') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_P); robot.keyRelease(KeyEvent.VK_P); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'Q') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_Q); robot.keyRelease(KeyEvent.VK_Q); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'R') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_R); robot.keyRelease(KeyEvent.VK_R); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'S') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_S); robot.keyRelease(KeyEvent.VK_S); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'T') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_T); robot.keyRelease(KeyEvent.VK_T); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'U') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_U); robot.keyRelease(KeyEvent.VK_U); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'V') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'W') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_W); robot.keyRelease(KeyEvent.VK_W); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'X') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_X); robot.keyRelease(KeyEvent.VK_X); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'Y') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_Y); robot.keyRelease(KeyEvent.VK_Y); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'Z') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_Z); robot.keyRelease(KeyEvent.VK_Z); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == ' ') { robot.keyPress(KeyEvent.VK_SPACE); robot.keyRelease(KeyEvent.VK_SPACE); } else if (character == '0') { robot.keyPress(KeyEvent.VK_0); robot.keyRelease(KeyEvent.VK_0); } else if (character == '1') { robot.keyPress(KeyEvent.VK_1); robot.keyRelease(KeyEvent.VK_1); } else if (character == '2') { robot.keyPress(KeyEvent.VK_2); robot.keyRelease(KeyEvent.VK_2); } else if (character == '3') { robot.keyPress(KeyEvent.VK_3); robot.keyRelease(KeyEvent.VK_3); } else if (character == '4') { robot.keyPress(KeyEvent.VK_4); robot.keyRelease(KeyEvent.VK_4); } else if (character == '5') { robot.keyPress(KeyEvent.VK_5); robot.keyRelease(KeyEvent.VK_5); } else if (character == '6') { robot.keyPress(KeyEvent.VK_6); robot.keyRelease(KeyEvent.VK_6); } else if (character == '7') { robot.keyPress(KeyEvent.VK_7); robot.keyRelease(KeyEvent.VK_7); } else if (character == '8') { robot.keyPress(KeyEvent.VK_8); robot.keyRelease(KeyEvent.VK_8); } else if (character == '9') { robot.keyPress(KeyEvent.VK_9); robot.keyRelease(KeyEvent.VK_9); } else if (character == '`') { robot.keyPress(KeyEvent.VK_BACK_QUOTE); robot.keyRelease(KeyEvent.VK_BACK_QUOTE); } else if (character == '-') { robot.keyPress(KeyEvent.VK_MINUS); robot.keyRelease(KeyEvent.VK_MINUS); } else if (character == '=') { robot.keyPress(KeyEvent.VK_EQUALS); robot.keyRelease(KeyEvent.VK_EQUALS); } else if (character == '~') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_BACK_QUOTE); robot.keyRelease(KeyEvent.VK_BACK_QUOTE); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == '!') { robot.keyPress(KeyEvent.VK_EXCLAMATION_MARK); robot.keyRelease(KeyEvent.VK_EXCLAMATION_MARK); } else if (character == '@') { robot.keyPress(KeyEvent.VK_AT); robot.keyRelease(KeyEvent.VK_AT); } else if (character == '#') { robot.keyPress(KeyEvent.VK_NUMBER_SIGN); robot.keyRelease(KeyEvent.VK_NUMBER_SIGN); } else if (character == '$') { robot.keyPress(KeyEvent.VK_DOLLAR); robot.keyRelease(KeyEvent.VK_DOLLAR); } else if (character == '%') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_5); robot.keyRelease(KeyEvent.VK_5); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == '^') { robot.keyPress(KeyEvent.VK_CIRCUMFLEX); robot.keyRelease(KeyEvent.VK_CIRCUMFLEX); } else if (character == '&') { robot.keyPress(KeyEvent.VK_AMPERSAND); robot.keyRelease(KeyEvent.VK_AMPERSAND); } else if (character == '*') { robot.keyPress(KeyEvent.VK_ASTERISK); robot.keyRelease(KeyEvent.VK_ASTERISK); } else if (character == '(') { robot.keyPress(KeyEvent.VK_LEFT_PARENTHESIS); robot.keyRelease(KeyEvent.VK_LEFT_PARENTHESIS); } else if (character == ')') { robot.keyPress(KeyEvent.VK_RIGHT_PARENTHESIS); robot.keyRelease(KeyEvent.VK_RIGHT_PARENTHESIS); } else if (character == '_') { robot.keyPress(KeyEvent.VK_UNDERSCORE); robot.keyRelease(KeyEvent.VK_UNDERSCORE); } else if (character == '+') { robot.keyPress(KeyEvent.VK_PLUS); robot.keyRelease(KeyEvent.VK_PLUS); } else if (character == '\t') { robot.keyPress(KeyEvent.VK_TAB); robot.keyRelease(KeyEvent.VK_TAB); } else if (character == '\n') { robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); } else if (character == '[') { robot.keyPress(KeyEvent.VK_OPEN_BRACKET); robot.keyRelease(KeyEvent.VK_OPEN_BRACKET); } else if (character == ']') { robot.keyPress(KeyEvent.VK_CLOSE_BRACKET); robot.keyRelease(KeyEvent.VK_CLOSE_BRACKET); } else if (character == '\\') { robot.keyPress(KeyEvent.VK_BACK_SLASH); robot.keyRelease(KeyEvent.VK_BACK_SLASH); } else if (character == '{') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_OPEN_BRACKET); robot.keyRelease(KeyEvent.VK_OPEN_BRACKET); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == '}') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_CLOSE_BRACKET); robot.keyRelease(KeyEvent.VK_CLOSE_BRACKET); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == '|') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_BACK_SLASH); robot.keyRelease(KeyEvent.VK_BACK_SLASH); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == ';') { robot.keyPress(KeyEvent.VK_SEMICOLON); robot.keyRelease(KeyEvent.VK_SEMICOLON); } else if (character == ':') { robot.keyPress(KeyEvent.VK_COLON); robot.keyRelease(KeyEvent.VK_COLON); } else if (character == '\'') { robot.keyPress(KeyEvent.VK_QUOTE); robot.keyRelease(KeyEvent.VK_QUOTE); } else if (character == '"') { robot.keyPress(KeyEvent.VK_QUOTEDBL); robot.keyRelease(KeyEvent.VK_QUOTEDBL); } else if (character == ',') { robot.keyPress(KeyEvent.VK_COMMA); robot.keyRelease(KeyEvent.VK_COMMA); } else if (character == '.') { robot.keyPress(KeyEvent.VK_PERIOD); robot.keyRelease(KeyEvent.VK_PERIOD); } else if (character == '/') { robot.keyPress(KeyEvent.VK_SLASH); robot.keyRelease(KeyEvent.VK_SLASH); } else if (character == '<') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_COMMA); robot.keyRelease(KeyEvent.VK_COMMA); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == '>') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_PERIOD); robot.keyRelease(KeyEvent.VK_PERIOD); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == '?') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_SLASH); robot.keyRelease(KeyEvent.VK_SLASH); robot.keyRelease(KeyEvent.VK_SHIFT); } robot.delay(25); } catch(Exception e) { exit(); } }
/* Serial Functions */ void SerialPortConnect() { serialPorts = Serial.list(); // serialPorts değişkenine bilgisayarda açık olan tüm com portları sıralatıyoruz. } void SerialPortList() { for (int i = 0; i < serialPorts.length; i++) { println("Port Number : " + i + " : " + serialPorts[i]); // Sıralattığımız bütün portları konsolda yazdırıyoruz. } } void SerialConnect(String port, int baud) { serial = new Serial(this, port, baud); // Seçtiğimiz com portuna serial haberleşme için bağlanıyoruz. } void SerialRead() { if (serial.available() > 0 ) { // Serial mesaj geliyorsa serialReadString = serial.readStringUntil(nl); // serialReadString kısmına gelen mesajı ekliyoruz if (serialReadString != null && newserialReadString != serialReadString) { // mesaj boş değilse ve eski mesajdan farklı ise // background(0); StringToChar(serialReadString); // mesajı char haline getiriyoruz for (int i =0; i < serialReadChar.length; i++) { RobotKeyPress(serialReadChar[i]); // robotumuzu çağırıp tuşa basmasını sağlıyoruz } } } else { } }
/* String to Char */ void StringToChar(String str) { serialReadChar = str.toCharArray(); }
Tüm Kodlar Processing
import processing.serial.*; // Seri bağlantısı kurmak için kütüphane import java.awt.Robot; // Klavyeye otomatik tıklamak için olan kütüphane import java.awt.event.KeyEvent; // Klavyeyi simüle den kütüphane import java.io.IOException; // Sisteme erişmek için kütüphane /* Robot Variable */ Robot robot; // Klavyeye otomatik basmak için bir robot tanımlıyoruz /* Serial Variable */ String serialReadString = null; // Serialden gelen mesaj String newserialReadString = "0"; // Serialdan gelen mesaj aynı ise yazdırmamak için kullandığımız değişken ama bu projede kullanmadık char[] serialReadChar; // Serialden gelen mesajı harf harf bölmek için kullandığımız değişken int nl = 10; // bu sabit bu şekilde dursun ellemeyin, bende ne işe yarıyor bilmiyorum String[] serialPorts; // Cihazın bağlı olduğu tüm seri portları görmek için Serial serial; // Serial bağlantısı için olan kütüphane sınıfına erişim void setup() { surface.setLocation(9999,9999); // Uygulamayı arka planda çalıştırmayı beceremediğim için ekranın sağ el alt köşesine yolluyorum size(0, 0); // Ekran boyutunu genişlik ve yüksekliğini 0,0 olarak tanımlıyorum SerialPortConnect(); // Seri bağlantı için yazdığımız fonksiyonu çağırıyoruz SerialPortList(); // Sistemdeki portları listeliyoruz SerialConnect(serialPorts[0], 9600); // Burada serialPorts[0] arduinonun bağlı olduğu com portu, 9600 ise arduinodaki serialBaudrate değeri RobotCreate(); // robot oluşturuyoruz, klavyeye otomatik tıklaması için } void draw() { SerialRead(); // Serialden gelen mesajları okumak için olan fonksiyonu çağırıyoruz. surface.setLocation(9999, 9999); } /* Robot */ void RobotCreate() { try { robot = new Robot(); // Robot oluşturuyoruz robot.delay(1000); // 1 Saniye bekletiyoruz } catch(Exception e) { exit(); } } /* Aşağıda oluşturduğumuz robotun klavyedeki tuşları tanımlıyoruz ve gelen tuşa basmasını sağlıyoruz. */ void RobotKeyPress(char character) { try { if (character == 'a') { robot.keyPress(KeyEvent.VK_A); robot.keyRelease(KeyEvent.VK_A); } else if (character == 'b') { robot.keyPress(KeyEvent.VK_B); robot.keyRelease(KeyEvent.VK_B); } else if (character == 'c') { robot.keyPress(KeyEvent.VK_C); robot.keyRelease(KeyEvent.VK_C); } else if (character == 'd') { robot.keyPress(KeyEvent.VK_D); robot.keyRelease(KeyEvent.VK_D); } else if (character == 'e') { robot.keyPress(KeyEvent.VK_E); robot.keyRelease(KeyEvent.VK_E); } else if (character == 'f') { robot.keyPress(KeyEvent.VK_F); robot.keyRelease(KeyEvent.VK_F); } else if (character == 'g') { robot.keyPress(KeyEvent.VK_G); robot.keyRelease(KeyEvent.VK_G); } else if (character == 'h') { robot.keyPress(KeyEvent.VK_H); robot.keyRelease(KeyEvent.VK_H); } else if (character == 'i') { robot.keyPress(KeyEvent.VK_I); robot.keyRelease(KeyEvent.VK_I); } else if (character == 'j') { robot.keyPress(KeyEvent.VK_J); robot.keyRelease(KeyEvent.VK_J); } else if (character == 'k') { robot.keyPress(KeyEvent.VK_K); robot.keyRelease(KeyEvent.VK_K); } else if (character == 'l') { robot.keyPress(KeyEvent.VK_L); robot.keyRelease(KeyEvent.VK_L); } else if (character == 'm') { robot.keyPress(KeyEvent.VK_M); robot.keyRelease(KeyEvent.VK_M); } else if (character == 'n') { robot.keyPress(KeyEvent.VK_N); robot.keyRelease(KeyEvent.VK_N); } else if (character == 'o') { robot.keyPress(KeyEvent.VK_O); robot.keyRelease(KeyEvent.VK_O); } else if (character == 'p') { robot.keyPress(KeyEvent.VK_P); robot.keyRelease(KeyEvent.VK_P); } else if (character == 'q') { robot.keyPress(KeyEvent.VK_Q); robot.keyRelease(KeyEvent.VK_Q); } else if (character == 'r') { robot.keyPress(KeyEvent.VK_R); robot.keyRelease(KeyEvent.VK_R); } else if (character == 's') { robot.keyPress(KeyEvent.VK_S); robot.keyRelease(KeyEvent.VK_S); } else if (character == 't') { robot.keyPress(KeyEvent.VK_T); robot.keyRelease(KeyEvent.VK_T); } else if (character == 'u') { robot.keyPress(KeyEvent.VK_U); robot.keyRelease(KeyEvent.VK_U); } else if (character == 'v') { robot.keyPress(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_V); } else if (character == 'w') { robot.keyPress(KeyEvent.VK_W); robot.keyRelease(KeyEvent.VK_W); } else if (character == 'x') { robot.keyPress(KeyEvent.VK_X); robot.keyRelease(KeyEvent.VK_X); } else if (character == 'y') { robot.keyPress(KeyEvent.VK_Y); robot.keyRelease(KeyEvent.VK_Y); } else if (character == 'z') { robot.keyPress(KeyEvent.VK_Z); robot.keyRelease(KeyEvent.VK_Z); } else if (character == 'A') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_A); robot.keyRelease(KeyEvent.VK_A); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'B') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_B); robot.keyRelease(KeyEvent.VK_B); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'C') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_C); robot.keyRelease(KeyEvent.VK_C); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'D') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_D); robot.keyRelease(KeyEvent.VK_D); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'E') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_E); robot.keyRelease(KeyEvent.VK_E); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'F') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_F); robot.keyRelease(KeyEvent.VK_F); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'G') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_G); robot.keyRelease(KeyEvent.VK_G); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'H') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_H); robot.keyRelease(KeyEvent.VK_H); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'I') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_I); robot.keyRelease(KeyEvent.VK_I); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'J') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_J); robot.keyRelease(KeyEvent.VK_J); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'K') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_K); robot.keyRelease(KeyEvent.VK_K); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'L') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_L); robot.keyRelease(KeyEvent.VK_L); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'M') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_M); robot.keyRelease(KeyEvent.VK_M); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'N') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_N); robot.keyRelease(KeyEvent.VK_N); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'O') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_O); robot.keyRelease(KeyEvent.VK_O); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'P') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_P); robot.keyRelease(KeyEvent.VK_P); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'Q') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_Q); robot.keyRelease(KeyEvent.VK_Q); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'R') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_R); robot.keyRelease(KeyEvent.VK_R); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'S') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_S); robot.keyRelease(KeyEvent.VK_S); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'T') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_T); robot.keyRelease(KeyEvent.VK_T); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'U') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_U); robot.keyRelease(KeyEvent.VK_U); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'V') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'W') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_W); robot.keyRelease(KeyEvent.VK_W); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'X') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_X); robot.keyRelease(KeyEvent.VK_X); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'Y') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_Y); robot.keyRelease(KeyEvent.VK_Y); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == 'Z') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_Z); robot.keyRelease(KeyEvent.VK_Z); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == ' ') { robot.keyPress(KeyEvent.VK_SPACE); robot.keyRelease(KeyEvent.VK_SPACE); } else if (character == '0') { robot.keyPress(KeyEvent.VK_0); robot.keyRelease(KeyEvent.VK_0); } else if (character == '1') { robot.keyPress(KeyEvent.VK_1); robot.keyRelease(KeyEvent.VK_1); } else if (character == '2') { robot.keyPress(KeyEvent.VK_2); robot.keyRelease(KeyEvent.VK_2); } else if (character == '3') { robot.keyPress(KeyEvent.VK_3); robot.keyRelease(KeyEvent.VK_3); } else if (character == '4') { robot.keyPress(KeyEvent.VK_4); robot.keyRelease(KeyEvent.VK_4); } else if (character == '5') { robot.keyPress(KeyEvent.VK_5); robot.keyRelease(KeyEvent.VK_5); } else if (character == '6') { robot.keyPress(KeyEvent.VK_6); robot.keyRelease(KeyEvent.VK_6); } else if (character == '7') { robot.keyPress(KeyEvent.VK_7); robot.keyRelease(KeyEvent.VK_7); } else if (character == '8') { robot.keyPress(KeyEvent.VK_8); robot.keyRelease(KeyEvent.VK_8); } else if (character == '9') { robot.keyPress(KeyEvent.VK_9); robot.keyRelease(KeyEvent.VK_9); } else if (character == '`') { robot.keyPress(KeyEvent.VK_BACK_QUOTE); robot.keyRelease(KeyEvent.VK_BACK_QUOTE); } else if (character == '-') { robot.keyPress(KeyEvent.VK_MINUS); robot.keyRelease(KeyEvent.VK_MINUS); } else if (character == '=') { robot.keyPress(KeyEvent.VK_EQUALS); robot.keyRelease(KeyEvent.VK_EQUALS); } else if (character == '~') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_BACK_QUOTE); robot.keyRelease(KeyEvent.VK_BACK_QUOTE); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == '!') { robot.keyPress(KeyEvent.VK_EXCLAMATION_MARK); robot.keyRelease(KeyEvent.VK_EXCLAMATION_MARK); } else if (character == '@') { robot.keyPress(KeyEvent.VK_AT); robot.keyRelease(KeyEvent.VK_AT); } else if (character == '#') { robot.keyPress(KeyEvent.VK_NUMBER_SIGN); robot.keyRelease(KeyEvent.VK_NUMBER_SIGN); } else if (character == '$') { robot.keyPress(KeyEvent.VK_DOLLAR); robot.keyRelease(KeyEvent.VK_DOLLAR); } else if (character == '%') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_5); robot.keyRelease(KeyEvent.VK_5); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == '^') { robot.keyPress(KeyEvent.VK_CIRCUMFLEX); robot.keyRelease(KeyEvent.VK_CIRCUMFLEX); } else if (character == '&') { robot.keyPress(KeyEvent.VK_AMPERSAND); robot.keyRelease(KeyEvent.VK_AMPERSAND); } else if (character == '*') { robot.keyPress(KeyEvent.VK_ASTERISK); robot.keyRelease(KeyEvent.VK_ASTERISK); } else if (character == '(') { robot.keyPress(KeyEvent.VK_LEFT_PARENTHESIS); robot.keyRelease(KeyEvent.VK_LEFT_PARENTHESIS); } else if (character == ')') { robot.keyPress(KeyEvent.VK_RIGHT_PARENTHESIS); robot.keyRelease(KeyEvent.VK_RIGHT_PARENTHESIS); } else if (character == '_') { robot.keyPress(KeyEvent.VK_UNDERSCORE); robot.keyRelease(KeyEvent.VK_UNDERSCORE); } else if (character == '+') { robot.keyPress(KeyEvent.VK_PLUS); robot.keyRelease(KeyEvent.VK_PLUS); } else if (character == '\t') { robot.keyPress(KeyEvent.VK_TAB); robot.keyRelease(KeyEvent.VK_TAB); } else if (character == '\n') { robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); } else if (character == '[') { robot.keyPress(KeyEvent.VK_OPEN_BRACKET); robot.keyRelease(KeyEvent.VK_OPEN_BRACKET); } else if (character == ']') { robot.keyPress(KeyEvent.VK_CLOSE_BRACKET); robot.keyRelease(KeyEvent.VK_CLOSE_BRACKET); } else if (character == '\\') { robot.keyPress(KeyEvent.VK_BACK_SLASH); robot.keyRelease(KeyEvent.VK_BACK_SLASH); } else if (character == '{') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_OPEN_BRACKET); robot.keyRelease(KeyEvent.VK_OPEN_BRACKET); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == '}') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_CLOSE_BRACKET); robot.keyRelease(KeyEvent.VK_CLOSE_BRACKET); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == '|') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_BACK_SLASH); robot.keyRelease(KeyEvent.VK_BACK_SLASH); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == ';') { robot.keyPress(KeyEvent.VK_SEMICOLON); robot.keyRelease(KeyEvent.VK_SEMICOLON); } else if (character == ':') { robot.keyPress(KeyEvent.VK_COLON); robot.keyRelease(KeyEvent.VK_COLON); } else if (character == '\'') { robot.keyPress(KeyEvent.VK_QUOTE); robot.keyRelease(KeyEvent.VK_QUOTE); } else if (character == '"') { robot.keyPress(KeyEvent.VK_QUOTEDBL); robot.keyRelease(KeyEvent.VK_QUOTEDBL); } else if (character == ',') { robot.keyPress(KeyEvent.VK_COMMA); robot.keyRelease(KeyEvent.VK_COMMA); } else if (character == '.') { robot.keyPress(KeyEvent.VK_PERIOD); robot.keyRelease(KeyEvent.VK_PERIOD); } else if (character == '/') { robot.keyPress(KeyEvent.VK_SLASH); robot.keyRelease(KeyEvent.VK_SLASH); } else if (character == '<') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_COMMA); robot.keyRelease(KeyEvent.VK_COMMA); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == '>') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_PERIOD); robot.keyRelease(KeyEvent.VK_PERIOD); robot.keyRelease(KeyEvent.VK_SHIFT); } else if (character == '?') { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_SLASH); robot.keyRelease(KeyEvent.VK_SLASH); robot.keyRelease(KeyEvent.VK_SHIFT); } robot.delay(25); } catch(Exception e) { exit(); } } /* Serial Functions */ void SerialPortConnect() { serialPorts = Serial.list(); // serialPorts değişkenine bilgisayarda açık olan tüm com portları sıralatıyoruz. } void SerialPortList() { for (int i = 0; i < serialPorts.length; i++) { println("Port Number : " + i + " : " + serialPorts[i]); // Sıralattığımız bütün portları konsolda yazdırıyoruz. } } void SerialConnect(String port, int baud) { serial = new Serial(this, port, baud); // Seçtiğimiz com portuna serial haberleşme için bağlanıyoruz. } void SerialRead() { if (serial.available() > 0 ) { // Serial mesaj geliyorsa serialReadString = serial.readStringUntil(nl); // serialReadString kısmına gelen mesajı ekliyoruz if (serialReadString != null && newserialReadString != serialReadString) { // mesaj boş değilse ve eski mesajdan farklı ise // background(0); StringToChar(serialReadString); // mesajı char haline getiriyoruz for (int i =0; i < serialReadChar.length; i++) { RobotKeyPress(serialReadChar[i]); // robotumuzu çağırıp tuşa basmasını sağlıyoruz } } } else { } } /* String to Char */ void StringToChar(String str) { serialReadChar = str.toCharArray(); }
Yorum Gönder