simulate selection in windows C++ - c++

I have this in my Notepad:
hello
I want to simulate selecting in C++ using Windows' keybd_event function.
here is my code:
keybd_event(VK_SHIFT, 0, 0, 0);
for (size_t i = 0; i < 5; i++)
{
keybd_event(VK_LEFT, 0, 0, 0);
keybd_event(VK_LEFT, 0, KEYEVENTF_KEYUP, 0);
}
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
but after I run this, it didn't select anything, it just go to the start of the file. Why isn't this working?

Add KEYEVENTF_EXTENDEDKEY will select rightly.
https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-keybdinput#members
#include <windows.h>
void main()
{
Sleep(2000);
keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, 0), KEYEVENTF_EXTENDEDKEY, 0);
for (size_t i = 0; i < 5; i++)
{
keybd_event(VK_LEFT, 0, 0, 0);
keybd_event(VK_LEFT, 0, KEYEVENTF_KEYUP, 0);
Sleep(20);
}
keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, 0), KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
Use VK_SHIFT in keybd_event, There is a problem that shift cannot be released,I recommend you use SendInput instead of keybd_event.
For higher-level operations, I also recommend you use UI Automation.
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput
https://learn.microsoft.com/en-us/windows/win32/winauto/entry-uiauto-win32

Related

ctrl is still pressed even after the program is terminated

I create an algorithm that copies some data from one file and pastes it into the program.
To copy this data I use this:
keybd_event (VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY, 0);
sleep (500);
keybd_event (VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
keybd_event (0x43, 0, KEYEVENTF_EXTENDEDKEY, 0);
Sleep (1);
keybd_event (0x43, 0, KEYEVENTF_KEYUP, 0);
Everything works fine, but after I stop the program, ctrl is still pressed and I have to reset my pc to turn it off. I tried to change the order but still nothing.
keybd_event (VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event (0x43, 0, KEYEVENTF_EXTENDEDKEY, 0);
sleep (500);
keybd_event (VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
Sleep (1);
keybd_event (0x43, 0, KEYEVENTF_KEYUP, 0);
What should I do to fix this?

How do I send shift+down using winapi c++?

Using the following code
Sleep(3000);
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event(VK_DOWN, 0, 0, 0);
keybd_event(VK_DOWN, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
I expect windows to select a line of text if i place my cursor in an editor during the sleep (hence it is foreground window)
However this just moves the cursor down a line.
Using the following works but surely this isn't the way its supposed to be done.
Sleep(3000);
keybd_event(VK_SHIFT, 0, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_DOWN, 0, 0, 0);
keybd_event(VK_DOWN, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
It appears by using the KEYEVENTF_EXTENDEDKEY flag I can hold it down for the arrow key. But no matter how I try to release it doesn't work. But I can press and release it normally to clear the held shift key.

functions in class: one is seen, but the other one is ignored

I'm currently working on a little game using SFML and C++, but I have a problem. I have a class Character in character.h with 2 functions inside, but when I try to access these functions in an other file (Game.cpp), one works perfectly while the other acts as if it doesn't even exist. Since this is my first post, and I don't know how to properly showcase my code, so please tell me if I'm not clear enough.
Thanks to all of you and have a good day.
error message:
/****CHARACTER.H****/
#ifndef CHARACTER_H
#define CHARACTER_H
#include <SFML/Graphics.hpp>
using namespace std;
class Character{
public:
Character();
~Character();
void initPlayer(string& fileName, sf::IntRect rect);
void moveCharacter();
sf::Sprite m_sprite;
private:
sf::VertexArray m_vertices;
sf::Texture m_texture;
};
#endif
/****CHARACTER.CPP*****/
#include "/home/hichem/C++/sfml/Game Engine/character.h"
#include "string"
#include <iostream>
using namespace std;
Character::Character(){
}
Character::~Character(){
}
void Character::initPlayer(string& fileName, sf::IntRect rect){
if (!m_texture.loadFromFile(fileName, rect)){
cout << "failed to load image" << endl;
}
m_sprite.setTexture(m_texture);
m_sprite.setPosition(sf::Vector2f(400, 200));
}
void Character::moveCharacter(){
}
/****GAME.H****/
#ifndef GAME_H
#define GAME_H
#include <SFML/Graphics.hpp>
#include "character.h"
#include "tileMap.h"
#include "string"
class Game: public sf::Transformable{
public:
Game();
~Game();
void run(sf::RenderWindow &window);
private:
void event(sf::RenderWindow &window);
void update();
void draw(sf::RenderWindow &window);
TileMap carte;
Character player;
const int level_1[128] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
};
#endif // GAME_H
/****GAME.CPP****/
#include "Game.h"
using namespace std;
Game::Game(){
string a = "images/tiles.png";
carte.load(a, sf::Vector2u(50, 50), level_1, 16, 8);
string b = "images/player.png";
/*FUNCTION THAT WORKS FROM CHARACTER.H*/
player.initPlayer(b, sf::IntRect(0,0,50,50));
}
Game::~Game(){
//dtor
}
void Game::event(sf::RenderWindow &window){
sf::Event event;
while (window.pollEvent(event)){
if (event.type == sf::Event::Closed)
window.close();
/*FUNCTION THAT DOES NOT WORKS FROM CHARACTER.H*/
player.moveCharacter();
}
}
void Game::update(){
}
void Game::draw(sf::RenderWindow &window){
window.clear();
window.draw(carte);
window.draw(player.m_sprite);
window.display();
}
void Game::run(sf::RenderWindow &window){
while(window.isOpen()){
event(window);
update();
draw(window);
}
}
Thanks for your help guys!, I was really tired last night and didn't noticed that I was linking the wrong path in character.cpp

Input layout, access violation, error handling not working like i want it to

I'm learning DirectX 11, and I wanted to get my head around DirectX debugging because I got an access violation reading location error on line 199 (create inputLayout)
I am trying to get an error box with directX errors show up because I read somewhere that it is a good programming practice to have that box show up with information about errors
Any ideas?
Also, help with the input layout would be appreciated
ID3DBlob *VS, *PS;
#if defined(DEBUG) | defined(_DEBUG)
#ifndef HR
#define HR(x) \
{ \
HRESULT hr = (x); \
if(FAILED(hr)) \
{ \
DXTrace(__FILE__, (DWORD)__LINE__, hr, L#x, true); \
} \
}
#endif
#else
#ifndef HR
#define HR(x) (x)
#endif
#endif
D3DX11CompileFromFile(L"shaders.fx", 0, 0, "VS", "vs_5_0", 0, 0, 0, &VS, 0, 0);
D3DX11CompileFromFile(L"shaders.fx", 0, 0, "PS", "ps_5_0", 0, 0, 0, &PS, 0, 0);
device->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &vShader);
device->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &pShader);
VS->Release();
PS->Release();
context->VSSetShader(vShader, 0, 0);
context->PSSetShader(pShader, 0, 0);
// define the input layout
D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }
};
UINT numElements = ARRAYSIZE(layout);
//below gives me access violation error and says that &inputLayout is NULL
HR(device->CreateInputLayout(layout, numElements, VS->GetBufferPointer(), VS->GetBufferSize(), &inputLayout));
You are releasing the VS blob before you create the layout according to the code above. You need the original Vertex Shader binary blob at the time you create the Input Layout so that Direct3D can validate they match up.
A simple fix is to move VS->Release(); to after you call CreateInputLayout.
A better answer is to remove all explicit use of Release an instead rely on a smart-pointer like Microsoft::WRL::ComPtr.
#include <wrl/client.h>
using Microsoft::WRL::ComPtr;
...
ComPtr<ID3DBlob> VS, PS;
...
D3DX11CompileFromFile(L"shaders.fx", 0, 0, "VS", "vs_5_0", 0, 0, 0, &VS, 0, 0);
D3DX11CompileFromFile(L"shaders.fx", 0, 0, "PS", "ps_5_0", 0, 0, 0, &PS, 0, 0);
device->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &vShader);
device->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &pShader);
context->VSSetShader(vShader, 0, 0);
context->PSSetShader(pShader, 0, 0);
...
//below gives me access violation error and says that &inputLayout is NULL
HR(device->CreateInputLayout(layout, numElements, VS->GetBufferPointer(), VS->GetBufferSize()));
Whenever VS and PS go out of scope, they will take clear of cleaning themselves up.

Why is the size and values of this OpenCV Matrix incorrect?

I use the following function to return a weight matrix of values between 0 and 1 depending on how close to the center a particular matrix position is. Also after a threshold the values are all 1 (it is like plateau with the points closer to center having value 1 and points away from center after a threshold of distance linearly fall from 1 to 0 at the edges)?
cv::Mat2f getWeightsMatrix(int N, int M, float r){
cv::Mat2f weights = cv::Mat2f(N,M);
int i,j;
for(i=0;i<N;i++){
for(j=0;j<M;j++){
if(i<=floor(N*(1-r)/2)){
if(j<=floor(M*(1-r)/2)){
weights[i][j]=((float)(i/N-j/M)/(1-r));
}
else{
weights[i][j]=(2*(float)(i/N)/(1-r));
}
}
else if (i>=floor(N*(1+r)/2)){
if(j>=floor(M*(1+r)/2)){
weights[i][j]=(((float)((N-i)/N))-((float)((M-j)/M)))/(1-r);
}
else{
weights[i][j]=(2*(float)((N-i)/N)/(1-r));
}
}
else{
if(j<=floor(M*(1-r)/2)){
weights[i][j]=(2*(float)(j/M)/(1-r));
}
else if(j>=floor(M*(1+r)/2)){
weights[i][j]=(2*(float)((M-j)/M)/(1-r));
}
else{
weights[i][j]=1;
}
}
}
}
cout << weights << endl;
return weights;
}
Now my problem is that I am having some casting issues and only values 0 and 1 are being returned (no floats). Also my matrix size displayed by the cout is 20x10 when I call the function with N=10, M=10 and r=0.5.
Please helP!
EDIT: This is the output
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Your matrix consists only if integers because when you do basic operations on integers, the results gets rounded automatically.
For example: when you write
weights[i][j]=((float)(i/N-j/M)/(1-r));
The result of i/N is rounded to a integer, j/M is also rounded to an integer, and finally, the division by (1-r) is also rounded. Your (float) cast is a good idea but it's applied too late.
You can do several things:
Cast inside elementary operations, for instance, float(i)/N instead of i/N
Use float numbers instead of integers: write 1.0 instead of 1
Use floats inside your loops and statements
For example:
for(float i = 0; i < N-0.5; i++) {
for(float j = 0; j < M-0.5; j++) {
if(i <= floor(N*(1.0-r)/2.0)) {
// ...
It's important you understand that because of floating point precision, a test such as i < N might or might not pass when float i = N. This is why I did a little trick by substracting 0.5 from your loop bounds N and M.