How to disable input method in C++ console? - c++

I'm making a text-based console game.
The Chinese input method is annoying as it blocks normal key input.
I tried to send NULL to ImmAssociateContext but it doesn't work.
#include<Windows.h>
#include <imm.h>
#include <atlstr.h>
#include<handleapi.h>
#include<iostream>
#pragma comment ( lib,"imm32.lib" )
using namespace std;
int main(int argc, char* argv[])
{
SetConsoleTitle(TEXT("test"));
char ch;
ImmAssociateContext(FindWindow(NULL, TEXT("test")), NULL);
cin >> ch;
cout << ch;
return 0;
}
Edit:
I don't need to input Chinese in this game.
About Chinese input:
When you input a letter in console, Chinese input methods would receive the input as pinyin, and turn it into Chinese characters.
(In the screenshot, aabbcc with dashline below is pinyin)
The same thing happened to Japanese input method.
This is not what I need. All I want is, when I press A, the console receives A.
About the code:
I'm using PDCurses in my project to draw a text-based gui and get key inputs.
Everything looks fine when the input method is turned off.
The code above shows ImmAssociateContext (google says it can turn off input method) doesn't work to me.

Related

Is there a way to change color without outputting the ANSI escape sequence in C++?

I am on android, using termux and clang. I am trying to do something like the echo command but with colored output (for fun.)
I have this code, and the normal outputting works, but I'm confused on how to change color without the ANSI escape sequence being outputted with the text?
This is my code:
#define RESET "\033[0m"
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
for (int i = 2; i < argc; ++i) {
cout << argv[1] << argv[i] << " " << RESET;
}
}
You will need to use a library that understands how to write to the terminal.
You could look at ncurses.
Its been a while since I did any of this stuff but if you want to draw (term used loosely) on the terminal this is a simple library that abstracts away particular terminal devices.
It is used by people writing OS installers who don't know what graphics software is available and so must write user interfaces that work in the terminal.
Here is a getting stared guide:
Hello World
ncurses Colored Text
Getting Started Guid

Farsi character utf8 in c++

i m trying to read and write Farsi characters in c++ and i want to show them in CMD
first thing i fix is Font i add Farsi Character to that and now i can write on the screen for example ب (uni : $0628) with this code:
#include <iostream>
#include <io.h>
#include <fcntl.h>
using namespace std;
int main() {
_setmode(_fileno(stdout), _O_U16TEXT);
wcout << L"\u0628 \n";
wcout << L"ب"<<endl;
system("pause");
}
but how i can keep this character ... for Latin characters we can use char or string but how about Farsi character utf8 ?!
and how i can get them ... for Latin characters we use cin>>or gets_s
should i use wchar_t? if yes how?
because with this code it show wrong character ...
wchar_t a='\u0628';
wcout <<a;
and i can't show this character بـ (uni $FE91) even though that exist in my installed font but ب (uni $0628) showed correctly
thanks in advance
The solution is the following line:
wchar_t a=L'\u0628';
The use of L tells the compiler that your type char is a wide char ("large" type, I guess? At least that's how I remember it) and this makes sure the value doesn't get truncated to 8 bits - thus this works as intended.
UPDATE
If you are building/running this as a console application in Windows you need to manage your code pages accordingly. The following code worked for me when using Cyrillic input (Windows code page 1251) when I set the proper code page before wcin and cout calls, basically at the very top of my main():
SetConsoleOutputCP(1251);
SetConsoleCP(1251);
For Farsi I'd expect you should use code page 1256.
Full test code for your reference:
#include <iostream>
#include <Windows.h>
using namespace std;
void main()
{
SetConsoleOutputCP(1256); // to manage console output
SetConsoleCP(1256); // to properly process console input
wchar_t b;
wcin >> b;
wcout << b << endl;
}

CMD Prompt C++: Limiting literals entered on screen

I hope the question isn't to ambiguous.
when I ask:
int main()
{
string name = {""};
cout << "Please enter a name: " << endl;
getline(cin, name);
//user enters 12 characters stop displaying next literal keypresses.
enter code here
}
I would like to be able to limit the amount of times the user can enter a char on screen. Example, the screen stops displaying characters after length 12?
If so what would be the library and command line for doing something like this?
Wanting to this as, I have a ascii art drawn on the CMD, and when I cout the statement at x,y anything over 12 characters long inputed draws over the ascii art.
I hope this makes sense :'{ Thank you!
By default the console is in cooked mode (canonical mode, line mode, ...). This means
that the console driver is buffering data before it hands it to your application
characters will be automatically echoed back to the console by the console driver
Normally, this means that your program only ever gets hold of the input after a line ends, i.e. when enter is pressed. Because of the auto-echo, those character are then already on screen.
Both settings can be changed independently, however the mechanism is --unfortunately-- an OS-specific call:
For Window it's SetConsoleMode():
HANDLE h_stdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode = 0;
// get chars immediately
GetConsoleMode(hStdin, &mode);
SetConsoleMode(hStdin, mode & ~ENABLE_LINE_INPUT));
// display input echo, set after 12th char.
GetConsoleMode(hStdin, &mode);
SetConsoleMode(hStdin, mode & ~ENABLE_ECHO_INPUT));
As noted by yourself, Windows still provides conio.h including a non-echoing _getch() (with underscore, nowadays). You can always use that and manually echo the characters. _getch() simply wraps the console line mode on/off, echo on/off switch into a function.
Edit: There is meant to be an example on the use of _getch(), here. I'm a little to busy to get it done properly, I refrained from posting potentially buggy code.
Under *nix you will most likely want to use curses/termcap/terminfo. If you want a leaner approach, the low level routines are documented in termios/tty_ioctl:
#include <sys/types.h>
#include <termios.h>
struct termios tcattr;
// enable non-canonical mode, get raw chars as they are generated
tcgetattr(STDIN_FILENO, &tcattr);
tcattr.c_lflag &= ~ICANON;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &tcattr);
// disable echo
tcgetattr(STDIN_FILENO, &tcattr);
tcattr.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &tcattr);
You can use scanf("%c",&character) on a loop from 1 to 12 and append them to a pre-allocated buffer.
As in my comments, I mentioned a method I figured out using _getch(); and
displaying each char manually.
simplified version:
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
string name = "";
int main()
{
char temp;
cout << "Enter a string: ";
for (int i = 0; i < 12; i++) { //Replace 12 with character limit you want
temp = _getch();
name += temp;
cout << temp;
}
system("PAUSE");
}
This lets you cout each key-press as its pressed,
while concatenating each character pressed to a string called name.
Then later on in what ever program you use this in, you can display the full name as a single string type.

Opening a pdf and opening a specific page

I am trying to open a pdf at a specific page when the user enters the correct number, I've been looking around but am lost with the windows.h not sure what to do next, here's my code:
// DungeonsAndDragons.cpp : Defines the entry point for the console application. //
#include "stdafx.h"
#include <iostream>
#include "windows.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[]) {
int featsSpells;
int select1;
int select2;
int select3;
string exit;
cout << "******Brad's DnD Feat/Spell Glossary*********" << endl;
cout << "Are you looking for Feats[#1] or Spells[#2]" << endl;
cin >> featsSpells;
if (featsSpells == 1){
ShellExecute(GetDesktopWindow(), "open", Argv[1],NULL,NULL,SHOWNORMAL);
}
system("pause");
return 0;
}
The code does not compile because I get a complaint about "const char *" is incompatible with parameter of type "LPCWSTR".
There are several things you should change.
The compiler / editor error is because you are mixing data types. You need to convert the ARGV into a LPCWSTR, because this is what ShellExecute expect.
To make sure that ShellExecute finds the .pdf you can specify the full path to the file, for example C:\temp\foo.pdf (if hardcoded into the source code you need to specify the backslash twice).
In order to tell the Acrobat reader (I think not all other alternative pdf viewer support this) on which page you want to start, you can append #page=123. Adobe documents the list of possible parameters in a PDF in a - you guessed it ;-) - pdf file

Unicode Windows console application (WxDev-C++/minGW 4.6.1)

I'm trying to make simple multilingual Windows console app just for educational purposes. I'm using c++ lahguage with WxDev-C++/minGW 4.6.1 and I know this kind of question was asked like million times. I'v searched possibly entire internet and seen probably all forums, but nothing really helps.
Here's the sample working code:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
/* English version of Hello world */
wchar_t EN_helloWorld[] = L"Hello world!";
wcout << EN_helloWorld << endl;
cout << "\nPress the enter key to continue...";
cin.get();
return 0;
}
It works perfectly until I try put in some really wide character like "Ahoj světe!". The roblem is in "ě" which is '011B' in hexadecimal unicode. Compiler gives me this error: "Illegal byte sequence."
Not working code:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
/* Czech version of Hello world */
wchar_t CS_helloWorld[] = L"Ahoj světe!"; /* error: Illegal byte sequence */
wcout << CS_helloWorld << endl;
cout << "\nPress the enter key to continue...";
cin.get();
return 0;
}
I heard about things like #define UNICODE/_UNICODE, -municode or downloading wrappers for older minGW. I tried them but it doesn't work. May be I don't know how to use them properly. Anyway I need some help. In Visual studio it's simple task.
Big thanks for any response.
Apparently, using the standard output streams for UTF-16 does not work in MinGW.
I found that I could either use Windows API, or use UTF-8. See this other answer for code samples.
Here is an answer, not sure this will work for minGW.
Also there are some details specific to minGW here