下面的代码是什么意思?

发布于 2024-10-08 01:40:57 字数 655 浏览 0 评论 0原文

以下代码的含义是什么:

//Keyboard map
#define LEFT_ARROW      'P'
#define RIGHT_ARROW     'Q'
#define UP_ARROW        'K'
#define DOWN_ARROW      'L'
#define CANCEL          'F'
#define HOME            'A'
#define BLANK           'B'
#define SIGN_TOGGLE     'G'
#define KB_DECIMAL      'R'
#define KB_ZERO         'S'
#define KB_ONE          'C'
#define KB_TWO          'D'
#define KB_THREE        'E'
#define KB_FOUR         'H'
#define KB_FIVE         'I'
#define KB_SIX          'J'
#define KB_SEVEN        'M'
#define KB_EIGHT        'N'
#define KB_NINE         'O'
#define ENTER           'T'

有人可以解释它是如何工作的以及为什么以这种方式定义吗?

What is the meaning of following code :

//Keyboard map
#define LEFT_ARROW      'P'
#define RIGHT_ARROW     'Q'
#define UP_ARROW        'K'
#define DOWN_ARROW      'L'
#define CANCEL          'F'
#define HOME            'A'
#define BLANK           'B'
#define SIGN_TOGGLE     'G'
#define KB_DECIMAL      'R'
#define KB_ZERO         'S'
#define KB_ONE          'C'
#define KB_TWO          'D'
#define KB_THREE        'E'
#define KB_FOUR         'H'
#define KB_FIVE         'I'
#define KB_SIX          'J'
#define KB_SEVEN        'M'
#define KB_EIGHT        'N'
#define KB_NINE         'O'
#define ENTER           'T'

Could anybody explain how it works and why they defined in that way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

燕归巢 2024-10-15 01:40:57

这些只是常数。这意味着在编译源代码之前,预处理器将遍历源代码,并将 #define 后面的任何单词实例替换为右侧的字符。因此,如果代码中有这样一行:

char myChar = LEFT_ARROW;

预处理器会将该代码更改为:

char myChar = 'P';

在编译之前。

Those are just constants. It means that the preprocessor will go through the source code and replace any instance of the word following #define with the character on the right, before compiling the source code. So if there was a line like this in the code:

char myChar = LEFT_ARROW;

the preprocesor will change that code into:

char myChar = 'P';

before compiling.

栀子花开つ 2024-10-15 01:40:57

#define TOKEN REPLACEMENT 是一个预处理器指令,它替换所有出现的从语法上来说,将 TOKEN 转换为 REPLACMENT

代码片段的目的是为键盘绑定分配名称,这意味着,如果您说 if (key == KB_NINE),编译器将看到 if (key == 'O' )

正确使用预处理器的优点不仅在于可读性:它还可以提高可维护性,以防常量发生变化。

关键的定义看起来很无意义:例如,KB_ONE'C',但是,通过修改一处常量,敲几下键盘就可以解决这个问题。

另请参阅:http://en.wikipedia.org/wiki/C_preprocessor#Macro_definition_and_expansion

#define TOKEN REPLACEMENT is a preprocessor directive, it replaces all occurrences of TOKEN into REPLACEMENT, syntactically.

The purpose of your code snippet is to assign names to keyboard bindings, that means, if you say if (key == KB_NINE), the compiler will see if (key == 'O').

The advantage of using preprocessors correctly is not only readability: it also increases maintainability, in case the constants change.

The key definitions seems to be nonsense: for example, KB_ONE is 'C', however, this problem can be solved in a few keystrokes by modifying the constant in one place.

See also: http://en.wikipedia.org/wiki/C_preprocessor#Macro_definition_and_expansion

鸠魁 2024-10-15 01:40:57

这些按键位于键盘的右侧。 C 运行时中没有标准方法可以让程序识别这些击键。于是就出现了各种非标准扩展来解决这个问题。

一种方案是当按下此类扩展键时让 getch() 返回 0,然后下一次调用 getch() 返回该扩展键的键码。该键码可能是原始键盘扫描码。一切皆有可能,您必须了解原始键盘供应商和 CRT 供应商才能获得线索。显然,它是古老的,专有的键盘接口是计算新石器时代的绝佳供应商锁定策略。

These are the kind of keys you'd find on the right-hand side of a keyboard. There is no standard way in the C runtime to let a program recognize these keystrokes. So there have been all kinds of non-standard extensions to fix this problem.

One scheme is to let getch() return 0 when such an extended key is pressed, the next getch() call then returns a key code for that extended key. That key code could be the original keyboard scan code. Anything is possible, you'd have to know the original keyboard vendor and CRT supplier to have a clue. Clearly it is ancient, proprietary keyboard interfaces was an excellent vendor lock-in strategy back in the neolithic days of computing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文