I am trying to get current key state for key ALT?
But its not work,why?
Here is the code
#include <iostream>
#include <Windows.h>
bool KeyPressed(short p_key)
{
if (GetAsyncKeyState(p_key) & 0x8000)
{
std::cout << "KEYPRESSED";
return true;
}
else return false;
}
int main()
{
std::cout << "Test";
while (true)
{
KeyPressed(0x4A);
}
std::cout << "";
}
It should get the state everytime it check,but its not at all
Try to use something like this:
if (GetKeyState(VK_MENU) & 0x8000))
{
// ALT key is down.
}
VK_MENU is the virtual-key code for the ALT key
Found this information in following article:
https://learn.microsoft.com/en-us/windows/win32/learnwin32/keyboard-input
Related
I'm wondering how could I trigger a function when control + c is pressed but I want it not only in the program window but outside the window like in a browser, text pad and etc. Help would be appreciated.
This would be in C++
Thanks
You can do something like this on Windows:
char input = _getch();
if (input == '\x3')
{
std::cout << "Ctrl C pressed!" << std::endl;
//...
}
The above code will print "Ctrl C pressed!" when input == '\x3' (Control C).
Full example:
#include <iostream>
#include <conio.h>
void foo(char character)
{
std::cout << character << std::endl;
}
int main()
{
while (true)
{
char input = _getch();
if (input == '\x3')
{
foo(input);
}
}
}
By using GetAsyncKeyState it cannot detect '~' button (at the top left of the keyboard).
So there is any way to detect this button?
Or should I use another command ?
By the way I am using c++
The virtual key code of "~" is VK_OEM_3.
More virtual key codes can be referenced:
https://learn.microsoft.com/en-us/windows/desktop/inputdev/virtual-key-codes
How to use it, please refer to MSDN
A simple example:
#include <Windows.h>
#include <iostream>
using namespace std;
int main()
{
BOOL OEM_3 = FALSE;
while (1)
{
if (GetAsyncKeyState(VK_OEM_3) < 0 && OEM_3 == false)
{
//Press down
OEM_3 = true;
cout << "Press down" << endl;
}
if (GetAsyncKeyState(VK_OEM_3) >= 0 && OEM_3 == true)
{
//Release
OEM_3 = false;
cout << "Release" << endl;
}
}
return 0;
}
I'm a bit new to C++, so I beg your pardon for being a bit nooby.
Is there a function I can use to make the console pause until a specific key is pressed?
Example being:
#include <iostream>
using namespace std;
int main()
{
int i = 0;
if (specific key pressed) {
i = 1;
} else if (other key pressed) {
i = 2;
}
cout << i << endl;
return 0;
}
The console should output 1 if the right key is pressed, and 2 if another key is.
What you're trying to do is a bit more complex, C++ makes use of the cin stream where the input into the console is fed into your program. Where as a key-press event would be something the operating system would handle and would vary between operating systems. So using something like this would require the user to press enter/return for the input to be received by the program.
char key;
std::cin >> key;
if (key == 'a') {
std::cout << 1;
}
else {
std::cout << 2;
}
Find some answers here How to handle key press events in c++
Works on Windows only:
#include <iostream>
#include <vector>
#include <Windows.h>
char GetKey(std::vector<char> KeysToCheckFor)
{
while (true)
{
Sleep(1);
for (int i = 0; i < KeysToCheckFor.size(); i++)
{
if (GetKeyState(toupper(KeysToCheckFor[i])) < 0) { return KeysToCheckFor[i]; }
}
}
}
int main()
{
std::cout << "Press one of the keys: a,b,c\n";
char returnedkey = GetKey({ 'a', 'b', 'c' });
std::cout << returnedkey << " has been pressed!\n";
system("pause");
}
I am experimenting with keyboard input in SDL and I have encountered a strange problem. Whenever I get input it only outputs the appropriate response sometimes (Clicking X only sometimes closes the program, pressing 1 only sometimes outputs "you pressed 1". Here is my main code:
#include <iostream>
#include <SDL.h>
#include "Screen.h"
#include "Input.h"
using namespace std;
int main(int argc, char *argv[]) {
Screen screen;
Input input;
if (screen.init() == false) {
cout << "Failure initializing SDL" << endl;
}
while (true) {
if (input.check_event() == "1") {
cout << "You pressed 1" << endl;
} else if (input.check_event() == "quit") {
break;
}
}
SDL_Quit();
return 0;
and here is my Input class:
#include <iostream>
#include <SDL.h>
#include "Input.h"
using namespace std;
string Input::check_event() {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
return "quit";
}
else if(event.type == SDL_KEYDOWN){
switch(event.key.keysym.sym){
case SDLK_1:
return "1";
}
}
}
return "null";
}
Any help would be appreciated!
From the documentation of SDL_PollEvent():
If event is not NULL, the next event is removed from the queue and
stored in the SDL_Event structure pointed to by event.
Analyzing your code:
if (input.check_event() == "1") {
This removes the event, whatever it is, from the queue.
} else if (input.check_event() == "quit") {
Say the return value of the 1st call to check_event() was "quit", then this call won't return "quit" again, because this information is now lost.
To fix that, call check_event() only once per loop iteration and store the result in a temporary variable. Then use only that variable in the conditions:
while (true) {
string event = input.check_event();
if (event == "1") {
cout << "You pressed 1" << endl;
} else if (event == "quit") {
break;
}
}
I'm starting a project called Nice System Monitor aiming to monitor processes on Linux, and I'm using C++ and Qt with QtCreator.
I've started making a QThread with a function called to fill a QTableWidget repeatedly but the table doesn't update properly even if I delete each row before fulling it up again.
I'm quite new to Qt and inspired myself of different sources on the Internet.
Here's the code of the QThread :
#include <unistd.h>
#include <ios>
#include <iostream>
#include <fstream>
#include <string>
#include <dirent.h>
#include <stdio.h>
#include <cctype>
#include <stdlib.h>
#include <vector>
#include <sstream>
#include "renderprocesstablethread.h"
#include <proc/readproc.h>
#include <proc/procps.h>
#include "mainwindow.h"
using namespace std;
RenderProcessTableThread::RenderProcessTableThread(QObject *parent)
: QThread(parent)
{
restart = false;
abort = false;
}
RenderProcessTableThread::~RenderProcessTableThread()
{
mutex.lock();
abort = true;
condition.wakeOne();
mutex.unlock();
wait();
}
bool RenderProcessTableThread::isNum(char *s) {
int i = 0, flag;
while(s[i]){
//if there is a letter in a string then string is not a number
if(isalpha(s[i]) || s[i] == '.'){
flag = 0;
break;
}
else flag = 1;
i++;
}
if (flag == 1) return true;
else return false;
}
string RenderProcessTableThread::convertDouble(double value) {
std::ostringstream o;
if (!(o << value))
return "";
return o.str();
}
string RenderProcessTableThread::convertInt(int value) {
std::ostringstream o;
if (!(o << value))
return "";
return o.str();
}
void RenderProcessTableThread::run()
{
forever {
mutex.lock();
mutex.unlock();
fillProcessTable();
sleep(1000);
//cout << "ça marche" << endl;
}
mutex.lock();
if (!restart)
condition.wait(&mutex);
restart = false;
mutex.unlock();
}
void RenderProcessTableThread::setLocalMainWindow(MainWindow& w)
{
localMainWindow = &w;
ui_tableWidgetProcessus = localMainWindow->findChild<QTableWidget*>("tableWidgetProcessus");
ui_tableWidgetProcessus->setColumnCount(11);
ui_tableWidgetProcessus->setColumnWidth(10,508);
QFont fnt;
fnt.setPointSize(10);
fnt.setFamily("Arial");
ui_tableWidgetProcessus->setFont(fnt);
QStringList labels;
labels << "user" << "pid" << "cpu" << "nice" << "vsz" << "rss" << "tty" << "stat" << "start" << "time" << "cmd";
ui_tableWidgetProcessus->setHorizontalHeaderLabels(labels);
}
void RenderProcessTableThread::fillProcessTable() {
QMutexLocker locker(&mutex);
if (!isRunning()) {
start(LowPriority);
} else {
restart = true;
condition.wakeOne();
}
PROCTAB* proc = openproc(PROC_FILLUSR | PROC_FILLMEM | PROC_FILLSTAT | PROC_FILLSTATUS | PROC_FILLARG);
proc_t proc_info;
memset(&proc_info, 0, sizeof(proc_info));
int totalRow = ui_tableWidgetProcessus->rowCount();
for ( int i = 0; i < totalRow ; ++i )
{
ui_tableWidgetProcessus->removeRow(i);
}
int i = 0;
while (readproc(proc, &proc_info) != NULL) {
cout << proc_info.fuser << proc_info.tid << proc_info.cmd << proc_info.resident << proc_info.utime << proc_info.stime << endl;
ui_tableWidgetProcessus->setRowCount(i+1);
ui_tableWidgetProcessus->setItem(i,0,new QTableWidgetItem(QString(proc_info.fuser),0));
ui_tableWidgetProcessus->setItem(i,1,new QTableWidgetItem(QString((char*)convertInt(proc_info.tid).c_str()),0));
ui_tableWidgetProcessus->setItem(i,2,new QTableWidgetItem(QString((char*)convertInt(proc_info.pcpu).c_str()),0));
ui_tableWidgetProcessus->setItem(i,3,new QTableWidgetItem(QString((char*)convertInt(proc_info.nice).c_str()),0));
ui_tableWidgetProcessus->setItem(i,4,new QTableWidgetItem(QString((char*)convertInt(proc_info.vm_size).c_str()),0));
ui_tableWidgetProcessus->setItem(i,5,new QTableWidgetItem(QString((char*)convertInt(proc_info.rss).c_str()),0));
ui_tableWidgetProcessus->setItem(i,6,new QTableWidgetItem(QString((char*)convertInt(proc_info.tty).c_str()),0));
ui_tableWidgetProcessus->setItem(i,7,new QTableWidgetItem(QString(proc_info.state),0));
ui_tableWidgetProcessus->setItem(i,8,new QTableWidgetItem(QString((char*)convertInt(proc_info.start_time).c_str()),0));
ui_tableWidgetProcessus->setItem(i,9,new QTableWidgetItem(QString((char*)convertInt(proc_info.stime).c_str()),0));
//cout << "proc_info.tid : " << proc_info.tid << endl;
//cout << "proc_info.cmdline : " << proc_info.cmdline << endl;
string text;
if (proc_info.cmdline != 0) {
vector<string> v(proc_info.cmdline, proc_info.cmdline + sizeof(proc_info.cmdline) / sizeof(string));
text = v[0];
}
else {
vector<string> v;
v.push_back(proc_info.cmd);
text = v[0];
}
//string text = char_to_string(proc_info.cmdline);
ui_tableWidgetProcessus->setItem(i,10,new QTableWidgetItem(QString((char*)text.c_str()),0));
i++;
}
closeproc(proc);
}
Are they better ways of doing this ?
Thanks
Patrick
This looks like something for Qt's Signal and Slots.
In your case the the thread emits the signal and a slot in your window will be called.
So in your RenderProcessTableThread.h define a signal
signals:
void newValues(const QString &data);
And in your mainwindow.h
public slots:
void showNewValues(const QString &data);
add the data to your table in this slot.
Then you have to connect them (e. g. in the constructor of your mainwindow after the creation of the thread)
connect(yourThread, SIGNAL(newValues(QString)), this, SLOT(showNewValues(QString)));
Whenever you want to show new data, emit the signal (e. g. somewhere in your fillProcessTable() function):
emit newValues(yourValues);
Qt does the connection between the threads for you.