X11 Unicode KeyEvent Issues - c++

The program I'm working on a virtual keyboard of sorts that requires unicode. Using the code I've received from http://goo.gl/pv9ht and it works for normal (ASCII) keysyms when converting to a keycode, but XKeysymToKeycode() returns 0 on a keysym like XK_agrave (include/X11/keysymdefs.h).
I'm also not quite sure how to do the same with capitals. When I try the same with XK_A (that's capital 'A'), it returns the same keycode as 'a'. This does make sense since they are the same keycode (along with a bunch of other characters) according to the output 'xmodmap -pke'. But how do I make it send (XSendKeyEvent) the capital form of the keycode?
Help would be much appreciated.

You send with a modifier key (shift in this case)
So you set XKeyEvent->state |= ShiftMask
The state member is set to indicate the logical state of the pointer buttons and modifier keys just prior to the event, which is the bitwise inclusive OR of one or more of the button or modifier key masks: Button1Mask, Button2Mask, Button3Mask, Button4Mask, Button5Mask, ShiftMask, LockMask, ControlMask, Mod1Mask, Mod2Mask, Mod3Mask, Mod4Mask, and Mod5Mask.
Source: http://linux.die.net/man/3/xkeyevent

Related

ASCII character 27 (left arrow) disappears when using the system() function in C++

While writing a program I encountered a bug. When I don't use the system() function in my code, e.g. system("CLS"); character 27 (that is the left arrow in the ASCII code) is displayed correctly, but after using this function this character turns into an empty space. I should add that this does not happen with any other ASCII character.
Here is some code:
printf("%c %c",27,26);
And it displays: ← →
but
system("CLS");
printf("%c %c",27,26);
displays:   →
Anyone had a similar problem and dealt with it? I would like to find out how to solve it because I need both the system() and the left arrow.
The ← character in Unicode is codepoint U+2190 LEFTWARDS ARROW.
In codepage 437 (aka the DOS OEM codepage), for instance, byte 0x1B (dec 27) is used to display Unicode codepoint U+2190. See Console Code Pages for more details.
So, it is possible that maybe calling system("CLS") is resetting the current codepage of the terminal, which would explain why printing 0x1B no longer displays as ←. Try calling system("CHCP 437") or SetConsoleOutputCP(437) afterwards to change it back.
Or, you could simply stop relying on printing ANSI strings using codepages at all. Print out Unicode strings instead, using wprintf() or WriteConsoleW(), or since your question is tagged as c++, use std::wcout.
Out of interest, on the windows console, the ASCII codes 0-5, 11, 16-26 and 28-31 have printable symbols, the others below 32 really are control codes and may do something very odd 😁
char(27) really is escape and char(24) cancels it. char(7) sounds the bell.

ncurses: handle keyboard inputs in the terminal in C++

I am trying to build a simple tetris game within the terminal. I need to move the pieces when the player presses an arrow key. I heard about the getch() method but it doesn't seem to work with arrow keys (they were all detected with the same int code: 27). What should I do?
Here is my current code:
initscr();
int inputCode;
do
{
inputCode = getch();
// here I would put my code to move the pieces if the code is right
} while (inputCode != 113); // q to exit
endwin();
Sincerely,
Thomas
The as noted in the manual page:
Most programs would additionally use the sequence:
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
That is, your program should turn on keypad mode to get KEY_UP, etc:
The keypad option enables the keypad of the user's terminal. If enabled (bf is TRUE), the user can press a function key (such as an arrow
key) and wgetch(3x) returns a single value representing the function
key, as in KEY_LEFT. If disabled (bf is FALSE), curses does not treat
function keys specially and the program has to interpret the escape sequences itself. If the keypad in the terminal can be turned on (made
to transmit) and off (made to work locally), turning on this option
causes the terminal keypad to be turned on when wgetch(3x) is called.
The default value for keypad is FALSE.

Borland c++ console functions

I'm studing now and I got this homework / tasks to do:
1) If you press the CTRL + L key, all numeric symbols should change the color.
2) If you press the CTRL + S key, you will get the length of the word, left from the cursor.
I found this function int bioskey(int cmd);
So now I can check if the key is pressed, but how to change the color only of numeric symbols, or read words from console to get their length ?
Some of us still remember the MS-DOS (let it rest in peace or pieces...)
if you are really in MS-DOS then you can not expect that the content of the console would be changed in colors for only specific areas. You need to do that your self. The problem is we do not know anything about your project background so we do not know what and how yours stuff is represented,rendered/outputed/inputed etc...
I assume EGA/VGA BIOS text mode is used so you can exploit direct access to the VRAM. So you need to set pointer to the address B800:0000 and handle it as array where each character on screen has 2 BYTEs. one is color attribute and the other is ASCII code (not sure in which order anymore)...
So for already rendered stuff you just:
loop through whole screen
usually 80x25x2 Bytes
test each ASCII for alpha numeric value
so ASCII code >= '0' and code<='9' for numbers or add all the stuff you are considering as alphanumeric like code>' ' and code<='9'.
change colors for selected characters
just by changing the attribute byte.
When you put it together for numbers it will look like this:
char far *scr=(char far*)0x0B0000000;
int x,y,a;
for (a=0,y=0;y<25;y++)
for (x=0;x<80;x++,a+=2)
if ((scr[a+0]>='0')&&((scr[a+0]<='9'))
{
scr[a+1]=7; //attribute with the different color here
}
if it does not work than try swap scr[a+0] and scr[a+1]. If an exception occur then you are not in MS-DOS and you do not have access to VRAM. In that case use DOS-BOX or driver that allows access to memory like dllportio ...
For more info see some more or less related QA's:
Display an array of color in C
What is the best way to move an object on the screen?
If you got problem with the CTRL+Key detection not sure if in-build function in TC++ allows CTRL (was too long ago) then you can exploit BIOS or even hook up the keyboard ISR. See the second link where ISR for keyboard handler is there present... You can port it to C++ or google there must be a lot of examples out there especially TP7.0 (which is pascal but easily portable to TC++)

how to flush or clear GetAsyncKeyState's buffer

i'm using GetAsyncKeyState in an MFC application to check if Esc button is pressed,
but when i press on Esc button from dialog and use GetAsyncKeyState in a different dialog it returns nonzero because it's exists in the message queue.
how can i clear or flush GetAsyncKeyState's buffer or delete this message from message queue?
Thanks in advance.
The direct answer to your question would be just calling it a second time, discarding the value from the first time.
But I guess that what you really want to know is how to read the current status of the key, regardless of when you last checked. Since you wrote "returns nonzero" I believe you are not using it correctly.
You need to check for the bit with value 0x8000 because this one indicates whether it's pressed right now. The bit with the value 1 is the one which is set if the key was pressed since the last check, and that's the one tripping you over, so just ignore it and directly test for the bit with value 0x8000.
Example code:
if(GetKeyState(VK_RETURN) & 0x8000) yayReturnIsPressed();
Checking if(GetKeyState(VK_RETURN)) or if(GetKeyState(VK_RETURN) != 0 will not do what you want because it will be fulfilled if any of the bits in the return value are set.
In GetAsyncKeyState documentation you can read:
If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState.
(emphasis mine)
so to check current state of ESC button you should only check most significant bit:
bool isEscPressed = GetAsyncKeyState(VK_ESCAPE) & 0x8000;
if you check state like that: if (GetAsyncKeyState(VK_ESCAPE)) {} then it will enter if statement even if ESC is not currently pressed.

Can't find a list of hex key when using modifers (shift, alt, ctrl) C++

RegisterHotKey(0, ZERO_KEYID, 0, 0x60) // Registers as numpad 0
Ok I am trying to set up hotkeys and As you can see above I need the hex value for the keyboard. I can find all the values without modifiers but how can I find when using modifiers such as shift, ctrl, alt?
A simple google request brings you Microsoft's website for RegisterHotKey and there you find it:
[https://msdn.microsoft.com/en-us/library/windows/desktop/ms646309%28v=vs.85%29.aspx][1]
The third parameter lets you specify the modifier(s) like:
RegisterHotKey (0, ZERO_KEYID, MOD_CONTROL|MODSHIFT, 0x60);
As far as I know there is no way of detecting when two arbitrary keys (like '1' and '2') are being pressed simultaneously.
Just use the MSDN documentation on RegisterHotKey.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms646309%28v=vs.85%29.aspx