What C++ header can I use for these specific functions ? Linux - c++

I am linux user and would like to use these 'keyboard_event' functions, but the header for these functions is 'windows.h' and linux doesn't have any 'windows.h', so can anyone support some alternative header for these functions, or alternative way to simulate key press for linux ?
#include <iostream>
using namespace std;
int main() {
keybd_event(VK_CONTROL,0x9d,0 , 0); //pressing CTRL
keybd_event(VkKeyScan(‘R’),0x93,0 , 0); //pressing 'R'
keybd_event(VkKeyScan(‘R’),0x93,KEYEVENTF_KEYUP,0); //releasing 'R'
keybd_event(VK_CONTROL,0x9d,KEYEVENTF_KEYUP,0); /* releasing CTRL */
return;
}

There's no "equivalent" for windows.h in Linux. You need to fix your errors case by case, or better, rewrite your code for Linux.
Reference: https://ubuntuforums.org/showthread.php?t=533304

The uinput kernel module and libevdev were introduced for exactly this purpose.

I've found a solution, in code I just type:
system("xte 'keydown Control_L' 'key R' 'keyup Control_L'");
and it does the same, but <cstdlib> has to be included.

Related

C++ Getting basic properties of a program

I am writing an algorithm that wants to check if google-chrome or has the same version as the input given by the user. for that I need a way to check what version google-chrome has. I am using a linux machine to program but I want to make it work at home where I use win 8.1
Is there a way to check in C/C++ what the version of a program is?
I thing it is best to get the awnser in a string because then i can just compare with
if(strcmp(version, input)=1)
Thanks for reading.
PS. I started in C++ but I can change, even to java if neccesary
here is the basic version of what i have now:
#include <iostream>
#include <string>
using namespace std;
#define x 256;
int main(){
std::string version;
std::string input;
//get version
if(strcmp(version, input)=1){
//versions are equal
}
//chrome needs to be updaded
return 0;
}
You can launch a terminal process from C with the popen() command. You'll need to include the stdio.h header. Here's a code snippet that might help you:
FILE *pd = popen("google-chrome --version", "r");
char output[50];
fgets(output,50,pd);
pclose(pd);
In the output array you'll get something like "Google Chrome 25.0.1364.97"

porting code from Linux to MinGW

I am writing a small class which can create/remove/rename/search for files and directories on the PC.
I successfully wrote the class and run on Linux.
When I was trying to run the same Class Code in MinGW, it was giving an error.
I could narrow down to:
mkdir function in Linux, Cygwin has 2 Arguments (directory name , mode permissions)
but in MinGW has only one argument(directory name).
My query is : a) What is the best way to make the code work on both OSs. b) Though I never used, I heard Preprocessor directives can be put like #ifdefined .....#endif ..or some thing of that sort c) Is using Preprocessor directives a good programming practice. As I learnt, preprocessor directives should be used minimally.
Could some one help me in this:
Here is my Code which works on Linux and Cygwin:
#include "BioDatabase.h"
#include <dirent.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
BioDatabase::BioDatabase() {
string s = getcwd(NULL,0);
changeDirectory(s,"*");
}
BioDatabase::BioDatabase(string directoryName, string extension)
{
changeDirectory(directoryName, extension);
}
bool BioDatabase::createDirectory(string st)
{
if( mkdir(st.c_str(),0755) == -1)
{
cerr <<endl<<"BOSERR-BioDatabase, createDirectory: Path or file function not found or Permission denied\n\n";
return false;
}
flag =1;
return true;
}
You could code something like
#if _POSIX_C_SOURCE
if( mkdir(st.c_str()) == -1)
#else
if ((mkdir(st.c_str(),0755) == -1)
#endif
See also feature_test_macros(7) man page.
1) you can use pre-processors to do one thing on one platform, and something different on another. EG:
#ifdef mingw32
/* windows specific code, like mkdir()... */
#else
/* other platform code, like a different way to call mkdir() */
#endif
2) Yes, you're absolutely right: limit using them as much as you can. but you'll quickly find out you can't avoid them entirely.
3) The best thing to do is to use a script that checks for
functionality rather than do it on a per-operating system basis.
Typically this involves writing a configure script (or
similar), which is a whole other learning curve. Still, it lets
you port to new platforms by checking for functionality rather than
adding the platform to a long list.

Clear Screen in Xcode

I am making a Library Management System in Xcode using C++. As Xcode does not support libraries such as conio.h and system "cls" does not work in it. What code should I use to clear the screen when I want it to shift from one menu to the other?
Check this out.
https://discussions.apple.com/thread/1064635?start=0&tstart=0
There is no direct way to do that; the system() command will not work on Mac (Unix). One option is to add a lot of spaces using code i.e.\n or other way is to use curses library
#include < curses.h > (curses.h) and then use system("clear"), which basically will do the same thing. So, its better to print spaces manually using the code rather than using some library.
One more thing you can do for POSIX (Unix, Linux, Mac OSX, etc) based systems [Note: I have not tested it myself]:
#include < unistd.h >
#include < term.h >
void ClearScreen()
{
if (!cur_term)
{
int result;
setupterm( NULL, STDOUT_FILENO, &result );
if (result <= 0) return;
}
putp( tigetstr( "clear" ) );
}
You'll have to link to the proper library (one of -lcurses, -lterminfo, etc.) to compile that last one. (Source: http://www.cplusplus.com/forum/articles/10515/)

Clearing the Console in C++

I am trying to clear the console in C++. I know printing a number of newlines is a bad practice, as it can be slow and is not always reliable to completely clear the console window, but I have researched multiple options and have found almost no other solutions besides system("cls"), which is an even worse option.
Essentially, I have used the line cout << string(100, '\n'); but I am getting a near-unidentifiable error when I try to run the program.
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)
I have also researched this, and found that most explanations were too complicated for me as a beginning C++ programmer to understand, or completely unrelated to my problem.
My questions are (1) is there a way to fix this error, and (2) could there be a better, cross-platform way of clearing the console other than printing 100 newlines?
I also heard of Console.clear(), but I'm unsure if this is cross-platform. From what I've seen, it looks more like a Windows command. I've also heard of the curses library, which I was willing to research and use, until I read somewhere that it was not recommended to use the functions which I am familiar with coupled with the curses library functions.
Thank you in advance!
About your error... you have to...
#include <iostream>
#include <string>
using namespace std;
If you are using just windows use windows console API.
If you are using a linux\unix terminal, use escape codes.
You can do a #if to choose between the two methods.
On linux\unix use the write function defined in in this way:
write(1,"\E[H\E[2J",7); // we use ANSI escape sequences here.
Here is the microsoft page that explain how to do that.
http://support.microsoft.com/kb/99261
The really bad console api microsoft use for the console always makes me angry :) why 100 lines of code to clear a screen? :)
Now the if... you should create a clearscreen.h file and a clearscreen.cpp file.
In clearscreen.h we just put our function.
void clearconsole();
In clearscreen.cpp we put our code for both operative systems
#ifdef _WIN32 || _WIN64
#include <windows.h>
void clearconsole()
{
...
// 100 lines of codes copied from microsoft article
}
#else
#include <unistd.h>
void clearconsole()
{
write(1,"\E[H\E[2J",7);
}
#endif
At a guess, your immediate problem is probably that you're missing an #include <string>.
Probably the most portable way of dealing with the screen is via ncurses. It's included in POSIX and most POSIX-like systems, and available as a library for most others (e.g., Windows) as well.
Edit: For what it's worth, clearing the screen on Windows doesn't require anywhere close to 100 lines of code.
#include <windows.h>
void clear_screen(char fill = ' ') {
COORD tl = {0,0};
CONSOLE_SCREEN_BUFFER_INFO s;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(console, &s);
DWORD written, cells = s.dwSize.X * s.dwSize.Y;
FillConsoleOutputCharacter(console, fill, cells, tl, &written);
FillConsoleOutputAttribute(console, s.wAttributes, cells, tl, &written);
SetConsoleCursorPosition(console, tl);
}
#ifdef TEST
int main(){
clear_screen();
return 0;
}
#endif
I'm the first to say that the code is more verbose than I'd like -- but it's less than ten lines, not to mention a hundred. Even the version in the MS knowledgebase is actually less than 40 lines -- of which many are blank or comments.
In fairness, however, I feel obliged to admit assembly language code writing directly to the hardware (or using the BIOS) does end up quite a bit shorter.
I know this is a complete necro. But I figured out what I feel is a rather neat solution and thought I'd share it just in case someone has this problem in the future.
void clearConsole() {
#ifdef _WIN32
#include <iostream>
static const char* CSI = "\33[";
printf("%s%c%s%c", CSI, 'H', CSI, '2J');
#else
#include <unistd.h>
write(1, "\E[H\E[2J", 7);
#endif
}

Problem setting desktop wallpaper using SystemParametersInfo Function

I am just learning C++ and am trying to write a small program to change the desktop wallpaper. Using the documentation here, I wrote this program:
#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "user32.lib")
void main(){
BOOL success = SystemParametersInfo(
SPI_SETDESKWALLPAPER, //iuAction
0, //uiParam
"C:\\test.jpg", //pvParam
SPIF_SENDCHANGE //fWinIni
);
if (success){
printf("Success!\n");
}else
printf("Failure =(\n");
}
The program always fails when I try to specify a file path for pvParam. It will correctly clear the wallpaper if I set pvParam to "". What am I doing wrong?
Thanks
-Abhorsen
It addition to Dennis' comment about JPEG files, it is also important whether or not you compile with UNICODE in effect. If you do then you'll have to specify the file as L"C:\test.jpg". Note the L in front of the string, that makes it a wide string. Or use SystemParametersInfoA(), note the A (but it's archaic).
Depending on the OS version, pvParam might not work.
If you are using Windows XP coupled with a JPEG file you are trying to assign as a wallpaper, notice the comment in the docs:
Windows Server 2003 and Windows
XP/2000: The pvParam parameter cannot
specify a .jpg file.