C/C++ 以及 Rust 中的 getch() 实现
getch 是一个在 C 语言编程中常用的函数,用于从键盘读取一个字符,但不回显到屏幕上。
在 Windows 环境下,getch 实现通常包含在 <conio.h> 头文件中。需要注意的是,getch 这个符号并非标准,标准的符号是 _getch,虽然 getch 一般会被指向 _getch,但你应当使用 _getch 而非 getch。
在 Unix/Linux 环境下,没有系统提供的 getch 实现,我们可以通过以下方法实现:
#include <termio.h>
int getch(void) {
struct termios tm, tm_old;
int fd = 0, ch;
if (tcgetattr(fd, &tm) < 0) { // 保存现在的终端设置
return -1;
}
tm_old = tm;
cfmakeraw(&tm); // 更改终端为原始模式,该模式下所有的输入数据以字节处理
if (tcsetattr(fd, TCSANOW, &tm) < 0) { // 设置上更改之后的设置
return -1;
}
ch = getchar();
if (tcsetattr(fd, TCSANOW, &tm_old) < 0) { // 更改设置为最初的样子
return -1;
}
return ch;
}
其中 struct termios,tcgetattr,tcsetattr,cfmakeraw 以及 getchar 的定义为:
typedef unsigned char cc_t;
typedef unsigned int speed_t;
typedef unsigned int tcflag_t;
#define NCCS 32
struct termios{
tcflag_t c_iflag; /* input mode flags */
tcflag_t c_oflag; /* output mode flags */
tcflag_t c_cflag; /* control mode flags */
tcflag_t c_lflag; /* local mode flags */
cc_t c_line; /* line discipline */
cc_t c_cc[NCCS]; /* control characters */
speed_t c_ispeed; /* input speed */
speed_t c_ospeed; /* output speed */
};
int tcgetattr(int __fd, struct termios *__termios_p);
void cfmakeraw(struct termios *__termios_p);
int tcsetattr(int __fd, int __optional_actions, const struct termios *__termios_p);
int getchar(void);
据此,我们可以通过 Rust 的 FFI 为 rust 实现一个 getch:
#[cfg(target_os = "windows")]
mod conio {
use std::os::raw::c_int;
extern "C" {
pub fn _getch() -> c_int;
}
}
#[cfg(target_os = "linux")]
#[allow(non_camel_case_types)]
mod conio {
use std::os::raw::c_int;
type tcflag_t = ::std::os::raw::c_uint;
type speed_t = ::std::os::raw::c_uint;
type cc_t = ::std::os::raw::c_uchar;
const NCCS: usize = 32;
const TCSANOW: i32 = 0;
#[repr(C)]
#[derive(Default, Copy, Clone)]
struct termios {
c_iflag: tcflag_t,
c_oflag: tcflag_t,
c_cflag: tcflag_t,
c_lflag: tcflag_t,
c_line: cc_t,
c_cc: [cc_t; NCCS],
c_ispeed: speed_t,
c_ospeed: speed_t,
}
extern "C" {
fn tcgetattr(__fd: c_int, __termios_p: *mut termios) -> c_int;
fn tcsetattr(__fd: c_int, __optional_actions: c_int, __termios_p: *const termios) -> c_int;
fn cfmakeraw(__termios_p: *mut termios);
fn getchar() -> c_int;
}
#[allow(unused_mut, unused_assignments)]
pub fn _getch() -> c_int {
unsafe {
let mut tm: termios = Default::default();
let mut tm_old: termios = Default::default();
let fd = 0;
let mut ch: c_int;
if tcgetattr(fd, &mut tm) < 0 {
return -1;
}
tm_old = tm;
cfmakeraw(&mut tm);
if tcsetattr(fd, TCSANOW, &mut tm) < 0 {
return -1;
}
ch = getchar();
if tcsetattr(fd, TCSANOW, &mut tm_old) < 0 {
return -1;
}
ch
}
}
}
#[allow(unused_unsafe)]
fn getch() -> char {
unsafe { conio::_getch() as u8 as char }
}
C/C++ 以及 Rust 中的 getch() 实现的更多相关文章
- Rust 中的继承与代码复用
在学习Rust过程中突然想到怎么实现继承,特别是用于代码复用的继承,于是在网上查了查,发现不是那么简单的. C++的继承 首先看看c++中是如何做的. 例如要做一个场景结点的Node类和一个Sprit ...
- Rust 中的类型转换
1. as 运算符 as 运算符有点像 C 中的强制类型转换,区别在于,它只能用于原始类型(i32 .i64 .f32 . f64 . u8 . u32 . char 等类型),并且它是安全的. 例 ...
- Rust中的RefCell和内部可变性
RefCell Rust在编译阶段会进行严格的借用规则检查,规则如下: 在任意给定时间,要么只能有一个可变引用,要么只能有多个不可变引用. 引用必须总是有效. 即在编译阶段,当有一个不可变值时,不能可 ...
- Rust中的Slices
这个slice切片,python中有,go中有, 但确实,Rust中最严格. 精彩见如下URL: Rust 程序设计语言(第二版) 简体中文版 · GitBook (Legacy) https://k ...
- 刷完欧拉计划中难度系数为5%的所有63道题,我学会了Rust中的哪些知识点?
我为什么学Rust? 2019年6月18日,Facebook发布了数字货币Libra的技术白皮书,我也第一时间体验了一下它的智能合约编程语言MOVE,发现这个MOVE是用Rust编写的,看来想准确理解 ...
- 【译】Rust中的array、vector和slice
原文链接:https://hashrust.com/blog/arrays-vectors-and-slices-in-rust/ 原文标题:Arrays, vectors and slices in ...
- 【译】理解Rust中的闭包
原文标题:Understanding Closures in Rust 原文链接:https://medium.com/swlh/understanding-closures-in-rust-21f2 ...
- 【译】理解Rust中的局部移动
原文标题:Understanding Partial Moves in Rust 原文链接:https://whileydave.com/2020/11/30/understanding-partia ...
- 【译】理解Rust中的Futures (一)
原文标题:Understanding Futures In Rust -- Part 1 原文链接:https://www.viget.com/articles/understanding-futur ...
- 【译】理解Rust中的Futures(二)
原文标题:Understanding Futures in Rust -- Part 2 原文链接:https://www.viget.com/articles/understanding-futur ...
随机推荐
- JVM性能优化, Part 5:Java的伸缩性
本文由 ImportNew - ImportNew读者 翻译自 Javaworld.如需转载本文,请先参见文章末尾处的转载要求. ImportNew注: JVM性能优化系列文章前4篇由ImportNe ...
- Windows下驱动安装
推荐使用金山毒霸中的电脑医生进行驱动或DLL文件的扫描,查找并下载 dll文件下载地址: https://www.wenjian.net/ 可以进行下载,告诉该文件的放置路径 其他:
- Sublime之快捷操作
列举常用的Sublime操作,涉及操作 1.每行默认需要统一添加逗号 1)全选 ctrl + a 2) 组合键 ctrl + shift + l 即可进行操作 (这里是L哦) 之后也可以使用HOME键 ...
- 2020-2021 ACM-ICPC Brazil Subregional Programming Contest
A. Sticker Album 你想要得到\(n\)张贴纸,每包礼物中等概率出现 \([A,B]\)范围内数量的贴纸,求需要买多少包礼物才能至少获得\(n\)张贴纸的期望次数 \(1 \leq n ...
- Echarts 提示组件
1.开启指示器 默认情况下,指示器是关闭状态,如果需要开启,直接配置tooltip字段即可 var option = { tooltip:{}, } 2.指示器的触发类型 触发类型的字段为trigge ...
- echarts 的使用
<template> // option 通过id行绑定 <div id="myRangChart" style="width: 100%;he ...
- (系列十四)Vue3+WebApi 搭建动态菜单
说明 该文章是属于OverallAuth2.0系列文章,每周更新一篇该系列文章(从0到1完成系统开发). 该系统文章,我会尽量说的非常详细,做到不管新手.老手都能看懂. 说明:OverallAuth2 ...
- openEuler欧拉安装Jenkins并修改构建workspace路径
一.系统优化 关闭防火墙 systemctl stop firewalld systemctl disable firewalld 关闭selinux sed -ri 's/SELINUX=enfo ...
- 叮咚~ 你的Techo大会云存储专场邀请函到了!
12月19日至20日,由腾讯主办的2020 Techo Park开发者大会将于北京召开.Techo Park 开发者大会是由腾讯发起的面向全球开发者和技术爱好者的年度盛会,作为一个专注于前沿技术研讨的 ...
- Win11 恢复 Win10 风格菜单
这个效果最好:https://github.com/valinet/ExplorerPatcher 不要入 Start11 之类的坑,也不需要改注册表,改注册表也改不出来.