1
我需要一個函數,如Objective C或Swift中的舊「getch()」,以從鍵盤讀取一個單獨的字符,在鍵入字符以使功能繼續之後沒有按下返回。Xcode Swift命令行工具從鍵盤讀取1個字符而不需要回顯或需要按回車鍵
這是,我知道,只有編程命令行工具,也許做一個選擇或編寫一個編輯器有趣。
我需要一個函數,如Objective C或Swift中的舊「getch()」,以從鍵盤讀取一個單獨的字符,在鍵入字符以使功能繼續之後沒有按下返回。Xcode Swift命令行工具從鍵盤讀取1個字符而不需要回顯或需要按回車鍵
這是,我知道,只有編程命令行工具,也許做一個選擇或編寫一個編輯器有趣。
在這裏被用於與夫特使用的功能,寫在夫特:
func GetKeyPress() -> Int
{
var key: Int = 0
var c: cc_t = 0
var cct = (c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c) // Set of 20 Special Characters
var oldt: termios = termios(c_iflag: 0, c_oflag: 0, c_cflag: 0, c_lflag: 0, c_cc: cct, c_ispeed: 0, c_ospeed: 0)
tcgetattr(STDIN_FILENO, &oldt) // 1473
var newt = oldt
newt.c_lflag = 1217 // Reset ICANON and Echo off
tcsetattr(STDIN_FILENO, TCSANOW, &newt)
key = Int(getchar()) // works like "getch()"
tcsetattr(STDIN_FILENO, TCSANOW, &oldt)
return key
}
下面是用於與Objective-C和夫特使用的功能,寫入C:
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>
int Get_Key (void) {
int key;
struct termios oldt, newt;
tcgetattr(STDIN_FILENO, &oldt); // 1473
memcpy((void *)&newt, (void *)&oldt, sizeof(struct termios));
newt.c_lflag &= ~(ICANON); // Reset ICANON
newt.c_lflag &= ~(ECHO); // Echo off, after these two .c_lflag = 1217
tcsetattr(STDIN_FILENO, TCSANOW, &newt); // 1217
key=getchar(); // works like "getch()"
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return key;
}