Change char inserted using Alt+Unicode in CRichEdit - c++

I want to change a unicode char inserted using Alt+Unicode code from the keyboard.
I used PretranslateMessage for changing the chars inserted directly from the keyboard and it worked. But with the Alt+Unicode code method it does not.
Here is the code:
Microsoft Word has this functionality when enabling show/hide paragraph marks.
BOOL CEmphasizeEdit::PreTranslateMessage(MSG* msg)
{
if (msg->hwnd == m_hWnd)
{
if (msg->message == WM_CHAR)
{
if (TheApp.Options.m_bShowWSpaceChars)
{
if (msg->wParam == ' ') // This works in both cases Space key pressed or Alt + 3 + 2 in inserted
{
msg->wParam = '·';
}
else if (msg->wParam == (unsigned char)' ') // this does not work
{
msg->wParam = (unsigned char)'°';
}
}
}
}
return CRichEditCtrl::PreTranslateMessage(msg);
}
If I insert from the keyboard Alt + 0 + 1 + 6 + 0 which is ' '(No-Break Space), I want the CRichEditCtrl to display '°' or another char that I specify.
How can I handle this to make it work?

Alt+Space is reserved for program's close menu.
You should use another sequence like Ctrl+Space or Alt+Ctrl+Space
' ' and (unsigned char)' ' are the same thing, therefore the code never reaches else if (msg->wParam == (unsigned char)' '). You should remove that.
Use GetAsyncKeyState to see if Alt or Ctrl key is pressed down.
BOOL IsKeyDown(int vkCode)
{
return GetAsyncKeyState(vkCode) & 0x8000;
}
...
if (msg->wParam == ' ')
{
if (IsKeyDown(VK_CONTROL))
msg->wParam = L'°';
else
msg->wParam = L'+';
}
...

I had to get the cursor position send an append string to the control and then set the selection after the inserted character. When this happens I have to skip CRichEditCtrl::PreTranslateMessage(msg);
BOOL CEmphasizeEdit::PreTranslateMessage(MSG* msg)
{
if (msg->hwnd == m_hWnd)
{
if (msg->message == WM_CHAR)
{
TCHAR text[2];
text[1] = 0x00;
BOOL found = 1;
switch (msg->wParam)
{
case 0x20: text[0] = _C('·'); break;
case 0xA0: text[0] = 0xB0; break;
}
CHARRANGE cr;
GetSel(cr);
cr.cpMax++;
cr.cpMin++;
ReplaceSel(text);
SetSel(cr);
return 1;
}
}
return CRichEditCtrl::PreTranslateMessage(msg);
}

Related

How do I detect non-letters with virtual codes in the Windows API?

In the Windows API and Direct2D/DirectWrite, I'm detecting the virtual code so text input in a 2D GUI can be appended. While it works fine, How can I include non-letters, such as !?., etc.
For example, when I press Shift+1, I get '1' instead of '!'. When I press '.', I get a boxed character. Can this detection be checked in this function somehow?
wchar_t TextBox::charIsPressed(int getKey)
{
char letter = getKey;
// Check for space character
if (letter == ' ')
return (wchar_t)letter;
// Check if the input is no letter
if ((getKey >= 'A') && (getKey <= 'Z'))
{
if (!GetAsyncKeyState(VK_SHIFT))
letter += 0x20;
}
return (wchar_t)letter;
}
It's calling function:
// Keyboard support
static X2D::Win32::KeyEvent *keyEvent;
if (m_focused)
{
// Check if there's editing space
if ((m_x + m_text.getWidth()) > (m_x + getWidth()))
return;
// Get the latest key event
keyEvent = frm.getKeyEvent();
if (!keyEvent->processed)
{
// Was backspace pressed?
if (keyEvent->virtual_code == VK_BACK)
{
m_text.setText(m_text.getText().substr(0, m_text.getText().length() - 1));
}
else if (keyEvent->virtual_code == VK_RETURN)
{
m_focused = false;
}
else
{
m_text.setText(m_text.getText() + charIsPressed(keyEvent->virtual_code));
}
keyEvent->processed = true;
}
}
Edit:
I found a way for detecting single characters, so it's a start.
// Converts '1' to '!'
if (getKey == '1')
{
if (GetAsyncKeyState(VK_SHIFT))
return '!';
}
Though typing '.' is getting me a semi-snowman Ascii figure.
Try something like this (this is Delphi, but it allows you to see the principle of translation):
function VKToChar(AVirtualCode: Word; out AChar: WideChar): Boolean;
var
KeyboardState: TKeyboardState;
ScanCode: DWORD;
Temp: UnicodeString;
Char: WideChar;
begin
AChar := #0;
Result := GetKeyboardState(KeyboardState);
if not Result then Exit;
ScanCode := MapVirtualKey(AVirtualCode, MAPVK_VK_TO_VSC);
SetLength(Temp, 3);
if ToUnicode(AVirtualCode, ScanCode, KeyboardState, PWideChar(Temp), Length(Temp), 0) = 1 then
begin
AChar := Temp[1];
Result := True;
end
else
Result := False;
end;

Octal code for "Ctrl + left arrow" and "Ctrl + right arrow"

I am working on a C++ game. I was trying to include character input using the getch() method. I need the character code (usually octal code) for Ctrl + ◀ and Ctrl + ▶ to move my objects in the game.
Just like we have a code for Ctrl + Z that is 032.
Here is part of the code I'm using.
if (kbhit())
{
gotoxy(x, y);
cout << h;
i = getch();
if (i == '***') //I need some code here to move my object
{
x=x-5;
}
if (i == '***') //also need here to move it to right ward
{
x=x+5;
}
if (i == LEFT && x>LEFTLIMIT)
{
x = x - 3;
}
else if (i == RIGHT && x<RIGHTLIMIT-6)
{
x = x + 3;
}
}
Using modifier keys I'd suggest using GetAsyncKeyState : MSDN
This way you can check for every key pressed currently or since last call of GetAsyncKeyState
if(GetAsyncKeyState(VK_LEFT) && GetAsyncKeyState(VK_CONTROL)) {
....
}

xlib XNextEvent checking if a key is held down

I am using xlib to get keyboard input I want to simulate windows its getAsynckeystate() to check if a button is being pressed I tried using a timer to fix the result but its still broken. the function should always return true if 'z' is held down even if other keys are pressed or released at the same time (not working right now)
Code below
bool KeyboardState::keyPressed(Display* d, Window curFocus,int revert, Window root) {
XEvent ev;
XNextEvent(d, &ev);
clock_t startTime;
switch (ev.type) {
case FocusOut:
if (curFocus != root)
XSelectInput(d, curFocus, 0);
XGetInputFocus(d, &curFocus, &revert);
printf("New focus is %d\n", (int) curFocus);
if (curFocus == PointerRoot)
curFocus = root;
XSelectInput(d, curFocus, KeyReleaseMask | FocusChangeMask | KeyPressMask);
break;
case KeyPress:
ks = XLookupKeysym(&(ev.xkey), 0);
if (ks == XK_z) {
keyState = true;
startTime = clock();
}
break;
case KeyRelease:
if(ks == XK_z && startTime - clock() > 0){
ks = XLookupKeysym(&(ev.xkey), 0);
keyState = false;
}
}
return keyState;
}
n.m linked following function which is accepted as answer:
tronche.com/gui/x/xlib/input/XQueryKeymap.html
I wrote the following code snippet to help do keyboard shortcuts with multiple non-modifier keys e.g. super + up + right.
#include <X11/Xlib.h>
// Returns 0 if key is not down;
// A non-zero integer if it is down.
// targetString should be appropriate for XStringToKeysym.
int is_down(Display *dis, char *targetString)
{
char keys_return[32] = {0};
KeySym targetSym = XStringToKeysym(targetString);
KeyCode targetCode = XKeysymToKeycode(dis, targetSym);
int targetByte = targetCode / 8;
int targetBit = targetCode % 8;
int targetMask = 0x01 << targetBit;
XQueryKeymap(dis, keys_return);
return keys_return[targetByte] & targetMask;
}
I've posted a more complete example with compilation instructions on GitHub. You can find a complete list of key names in a file named X11/keysymdef.h; on my Arch computer it was at /usr/include/X11/keysymdef.h. I also found an example on the web as the second Google result for x11 keysymdef.

C++ Parsing char array as a script file (syntax)

I have made a simple Script reading class in C++ which allows me to read and parse scripts.
Basically there's a FILE class, which then I proceed to open with "fopen".
In functions I proceed to call "fgetc" and "ftell" to parse the script file as needed, note this ain't an interpreter.
Every script file is supposed to follow a syntax, but this is why I'm asking here for a solution.
Here's how a script looks like:
# Script File Comment
USERNAME = "Joe"
PASSWORD = "pw0001"
ACCESSLEVEL = 3
DATABASE = ("localhost",3306,"db","user","password")
Basically I have a few functions:
// This function searches for "variables"
nextToken();
// After I have the variable, e.g: USERNAME, PASSWORD, ACCESSLEVEL or DATABASE
// I proceed to call this function
// This function reads the char array for (,-{}()[]=) these are symbols
readSymbol();
// In a condition I check what "token/variable" I got and proceed to read
// it accordingly
// e.g; for USERNAME I do:
readString(); // reads text inside "
// e.g; for ACCESSLEVEL I do:
readNumber(); // reads digits until the next char ain't a digit
// e.g; for DATABASE I do:
readSymbol(); // (
readString(); // 127.0.0.1
readSymbol(); // ,
readNumber(); // 3306
readSymbol(); // ,
readString(); // db
readSymbol(); // ,
readString(); // user
readSymbol(); // ,
readString(); // password
readSymbol(); // )
I would like to be able to read a variable declaration like this:
DATABASELIST = {"data1","data2","data3"}
or
DATABASELIST = {"data1"}
I could easily do readSymbol and readString to read for 3 different string definitions inside the variable, however this list is supposed to have custom user data, like 5 different strings, or 8 different strings - depends.
And I seriously have no idea how can I do this with the parser I wrote.
Please note that I am basing this in some Pseudo code I took from a scripter for this type of format, I have the pseudo code extracted from IDA, if you would like to see it for better understanding post here
Here's an example of my "readSymbol" function.
READSYMBOL
int TReadScriptFile::readSymbol()
{
int currentData = 0;
int stringStart = -1;
// Check if we can't read anymore
if (end)
return 0;
while (true)
{
// Basically get chars in the script
currentData = fgetc(File);
// Check for end of file
if (currentData == -1)
{
end = true;
break;
}
if (stringStart == -1)
{
if (isdigit(currentData) || isalpha(currentData))
{
printf("TReadScriptFile::readSymbol: Symbol expected\n");
close();
return 0;
}
else if
(
currentData == '=' || currentData == ',' ||
currentData == '(' || currentData == ')' ||
currentData == '{' || currentData == '}' ||
currentData == '>' || currentData == '<' ||
currentData == ':' || currentData == '-'
)
{
#ifdef __DEBUG__
printf("Symbol: %c\n", currentData);
#endif
stringStart = ftell(File);
break;
}
}
}
return 1;
}
NEXTTOKEN
int TReadScriptFile::nextToken()
{
int currentData = 0;
int stringStart = -1;
int stringEnd = -1;
RecursionDepth = -1;
memset(String, 0, 4000);
// Check if we can't read anymore
if (end)
return 0;
while (true)
{
// ** Syntax **
if (isdigit(getNext()) || getNext() == -1)
{
printf("No more tokens left.\n");
end = true;
close();
return 0;
}
// End
// Basically get chars in the script
currentData = fgetc(File);
// Check for end of file
if (currentData == -1)
{
end = true;
break;
}
// Syntax Checking Part, this really isn't needed but w/e
if (stringStart == -1)
{
if (currentData == '=' || isdigit(currentData))
{
printf("TReadScriptFile::nextToken: Syntax Error: string expected\n");
close();
return 0;
}
}
// End Syntax Checking
// It's a comment line, we should skip
if (currentData == '#')
{
seekNewLn();
continue;
}
// There are no variables, yet
if (stringStart == -1)
{
// We found a letter, we are near a token!
if (isalpha(currentData))
{
stringStart = ftell(File);
// We might as well add the letter to the string
RecursionDepth++;
String[RecursionDepth] = currentData;
continue;
}
}
else if (stringStart != -1)
{
// Let's wait until we get an identifier or space
// We found a digit, error
if (isdigit(currentData))
{
printf("TReadScriptFile::nextToken: string expected\n");
close();
return 0;
}
// We found a space, maybe we should stop looking for tokens?
else if (isspace(currentData))
{
#ifdef __DEBUG__
printf("Token: %s\n", String);
#endif
break;
}
RecursionDepth++;
String[RecursionDepth] = currentData;
}
}
return 1;
}
I found a good example of the approach I followed here:
http://llvm.org/docs/tutorial/LangImpl1.html
One mechanism to deal with DATABASE_LIST would be this:
After finding the variable DATABASE_LIST read a symbol using readSymbol() checking if it is a { then in a loop do readString() add it to a std::vector (or some other suitable container) then check for a , or } (using readSymbol()) . If it is a ,(comma) then you go back and read another string add to the vector etc. until you do finally reach } . When you are finished you'd have a vector (dynamic array) of strings that represent a DATABASE_LIST

Getting word under caret - C++, wxWidgets

I am writing a text editor using the wxWidgets framework. I need to get the word under caret from the text control. Here is what I came up with.
static bool IsWordBoundary(wxString& text)
{
return (text.Cmp(wxT(" ")) == 0 ||
text.Cmp(wxT('\n')) == 0 ||
text.Cmp(wxT('\t')) == 0 ||
text.Cmp(wxT('\r')) == 0);
}
static wxString GetWordUnderCaret(wxTextCtrl* control)
{
int insertion_point = control->GetInsertionPoint();
wxTextPos last_position = control->GetLastPosition();
int start_at, ends_at = 0;
// Finding starting position:
// from the current caret position, move back each character until
// we hit a word boundary.
int caret_pos = insertion_point;
start_at = caret_pos;
while (caret_pos)
{
wxString text = control->GetRange (caret_pos - 1, caret_pos);
if (IsWordBoundary (text)) {
break;
}
start_at = --caret_pos;
}
// Finding ending position:
// from the current caret position, move forward each character until
// we hit a word boundary.
caret_pos = ends_at = insertion_point;
while (caret_pos < last_position)
{
wxString text = control->GetRange (caret_pos, caret_pos + 1);
if (IsWordBoundary (text)) {
break;
}
ends_at = ++caret_pos;
}
return (control->GetRange (start_at, ends_at));
}
This code works as expected. But I am wondering is this the best way to approach the problem? Do you see any possible fixes on the above code?
Any help would be great!
Is punctuation part of a word? It is in your code -- is that what you want?
Here is how I would do it:
wxString word_boundary_marks = " \n\t\r";
wxString text_in_control = control->GetValue();
int ends_at = text_in_control.find_first_of( word_boundary_marks, insertion_point) - 1;
int start_at = text_in_control.Mid(0,insertion_point).find_last_of(word_boundary_marks) + 1;
I haven't tested this, so there likely are one or two "off-by-one" errors and you should add checks for "not found", end of string, and any other word markers. My code should give you the basis for what you need.