Linux C/C++ keyboard ctrl+[somekey] and ctrl+alt+[somekey} [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have been thinking how to listen keys and key combinations in Linux (and C/C++). My program structure is following:
Get text input. If ctrl + [somekey] is pressed, print text from a file called somekey.txt.
For example if you press ctrl + a the program should print the text from a file called a.txt.
If ctrl + alt + [somekey] pressed: the program saves already given (not pressed enter) text input to a file called somekey.txt. Example: ctrl+alt+a --> before typed text to file called a.txt. It should also replace the old text in the file if excist. It can be done by force-creating the file.
I have no idea which library I should use. Maybe sdl or ncurses? The program can also be written in some other languague as long as the program stays lightweight to run in Computers like Raspberry Pi. Could someone write me a little code which follows this structure? Thanks in (code) advice!

If you go down the SDL route, it should be pretty simple. In general, your program will have a flow like this:
Initialize SDL
Event loop
Wait for event
Handle event, possibly exiting loop
Shut down SDL
In that “handle events” step, you'll probably want to check to see if the event is a keyboard event, and if it is, switch on what modifier keys are held down. Then you can perform whatever logic is necessary.
See the documentation, and in particular the keyboard category for more information.

Related

Limited .exe Output in C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm sure this is a simple question, but I tried a couple google searches and wasn't sure what keywords to search.
After executing my program in C++ the .exe window that opens is only showing the bottom part of my output, while cutting off the top. Any thoughts on how to view all of my output?
Thank you.
You can redirect the output of the program to a file:
your-program.exe > file.txt
Alternatively, you can pipe the output into more:
your-program.exe | more
This will pause the output of your program when it fills one screen until you press the space bar.
Both approaches have their pros and cons: if you redirect the output to file and open that file while the program is running, you might not see the last chunk of data, because the OS might buffer the data before writing it to hard disk.
If you pipe the output into more then the execution of your program might be suspended while more is waiting for your input.
[Edit: incorporated enhzflep's suggestion of using a redirection to a file.]
suppose you have an "a.exe" program
execute the program like this:
a.exe >1.txt
and open the file "1.txt" with notepad or other editor(such as notepad++).

Alternative to system("PAUSE")? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I've recently read about how BAD system("PAUSE") is. I tried cin.get() but I don't know how to use it to pause a program. Other posts say to put a breakpoint after the statement. But I have no knowledge on how to do that. Any suggestions on how to pause my program? An example would be really appreciated.
Assuming that you are indeed working on Windows, the worst thing about system("PAUSE") is that it betrays a fundamental misunderstanding of your operating system's architecture. You do not need a code replacement for system("PAUSE"), because the code is the wrong place to solve the perceived problem.
Beginners like to put system("PAUSE") or even a portable alternative like std::cin.get() at the end of a program because otherwise "the window disappears" as soon as the program ends. Such logic, however, is deeply flawed. The window which you probably see while the program runs and which has made you ask this question is not part of the program itself but part of the environment in which the program runs.
A typical console program, however, must not assume details about the environment in which it is executed. You must instead learn to think in more abstract terms when it comes to input and output via std::cout and std::cin. Who says that your program is even visible for a human user? You may read from or write into a file; you may use pipes; you may send text to a network socket. You don't know.
#include <iostream>
int main()
{
std::cout << "Hello world\n"; // writes to screen, file, network socket...
}
Opening a graphical window and displaying text output on the screen is not in the scope of your program, yet using system("PAUSE") assumes exactly that one single use case and breaks all others.
If you use an IDE like Visual Studio and are annoyed by the fact that pressing F5 eventually results in the window disappearing before you have had the chance to see all output, here are three more sensible alternatives than manipulating the program itself:
Demystification. Observe that what Visual Studio really does is invoking the Visual C++ compiler behind the scenes in order to create an *.exe file. Open your own console window with cmd or with Tools > Visual Studio Command Prompt, locate the directory of that *.exe file and run it there (you should eventually also learn to start the compiler without Visual Studio's help, because that will give you a deeper understanding of the C++ build process).
Press CTRL+F5.
Place a breakpoint at the end of your code. Read the documentation if you don't know how.
I got one step closer, cin.ignore, but the user can only press enter if he/she wants to continue.
Example with cin.ignore():
#include <iostream>
int main()
{
cout << "Press enter to continue!\n";
cin.ignore();
//do something
return 0;
}
When you press enter it advances and does whatever you want it to do.

Is there a way to register hotkey on windows without conflict? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm writing a Windows program and I need to register a hotkey. However, I'm afraid of the conflicts caused by RegisterHotKey().
Furthermore, Key Events are blocked when you triggered a Hotkey message. For example, you call RegisterHotkey(0, 0, MOD_NOREPEAT | MOD_ALT, VK_UP) To register hot key ALT + ↑. If you press ALT + ↑ in explorer.exe, explorer.exe won't receive this message(Originally ALT + ↑ is go up a level in explorer.exe).
Is there a way to register hot key without conflicts and blocking?
The MSDN page for RegisterHotKey says:
RegisterHotKey fails if the keystrokes specified for the hot key have
already been registered by another hot key.
along with:
Return value
Type: BOOL
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error
information, call GetLastError.
In other words, if(!RegisterHotKey(...)) ... deal with error ... would catch various forms of "didn't work", including that the hotkey is already in use - you need to look at GetLastError to tell the difference between "key already in use" and others.
I think the whole point of registering a hotkey is that YOUR application ALWAYS gets that keypress, regardless of what is going on. Which of course is a bit annoying if it happens to be popular key in some other application, but it's still the point of registering a hotkey. If you don't like that effect, then don't register a hotkey, or use a more obscure combination that is less likely to be used by some other application. Not sure how else this can be solved - exactly what would you like the system to do if you run IE together with your application, and ALT-uparrow is pressed?

How Do I Give My C++ Application The File Path Of The File Clicked To Open Said Application [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an application that uses custom file types, and I have associated these file types with my application (Windows File System). When I double click the file my application opens, but I would like to be able to pass in the data from the file.
I had hoped that the file path would have been sent to main, but no such luck. Whenever I open the application by clicking on a file 'argc' reads '0'. Opening the application normally give 'argc' as '1'.
Is there a way to pass in the file path used to open the application?
Windows doesn't know a priori how your program should be started. Perhaps it's d:\path\to\exe\foo.exe --no-spash --input-file "C:\what you\clicked.foofile". That's why you put in a registry entry, to tell Windows.
Now there's of course one part of that string above which you don't know up front, and that's of course "C:\what you\clicked.foofile". In the registry entry, use "%1" and Windows will substitute the actual path. Don't forget the " " since paths can contain spaces, and else your path will end up in argv[1] and argv[2]. That was especially common in XP, with My Documents.
The path of the file calling will be stored in "char* argv[]" as either "argv[0]" or "argv[1]" (sorry for not remembering exactly, my C++ is a little rusty) . "int argc" simply lists how many elements exist in "char* argv[]"

How to continue program after another window is opened [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
So I am an amateur programmer, currently enrolled in my second semester of C++ (yesterday we learned about structs just to give you an idea of what I know...).
I am creating a simple program to help me file documents at my internship.
I am using a system command(yes I know its dangerous and a big security risk), to open a pdf in firefox so that I can see the file and know where to put it.
I have successfully opened firefox and opened the pdf.
However my program stops running until I close firefox.
My question is how do I continue my program at the same time?
Is there an easier way to display a pdf in an executable?
edit:
Here is the function I use to open the firefox window with the pdf in it:
void openPDFBrowser (char array[])
{
ofstream outFile;
outFile.close();
outFile.open("PDF_browswer_handleScript.txt") ;
if(outFile.good())cout<<"OUTFILE GOOD" << endl;
outFile << "system("<<array<<")"<<endl;
system("PDF_browswer_handleScript.txt");
outFile.close();
}
the .txt file contails: firefox C:\Scans\Attorney.pdf
where firefox references a .bat file which contains the location of firefox.exe
I will take any suggestions
it just seemed easier to use an external browser to handle the display of the pdf file, although I'm still working out this threading idea
system is a blocking command - meaning it will stop your execution until that function returns. The only way to do this is to create a separate thread (or to fork a separate process, as noted by Chris Hayes), and to run the system (or CreateProcess, or exec) inside it, allowing main thread to continue.