C++ Generating Key Combinations WINAPI (Without MFC)





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am trying to get my application to output a key combination ( ALT + D ) to focus on Internet explorers address bar, but I am having trouble implementing the code needed. I already have a method to pass 1 key:



void GenerateKey(int vk, BOOL bExtended) {

KEYBDINPUT kb = {0};
INPUT Input = {0};

/* Generate a "key down" */
if (bExtended) { kb.dwFlags = KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));

/* Generate a "key up" */
ZeroMemory(&kb, sizeof(KEYBDINPUT));
ZeroMemory(&Input, sizeof(INPUT));
kb.dwFlags = KEYEVENTF_KEYUP;
if (bExtended) { kb.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));

return;
}


Can anyone provide some help as to how to achieve a the desired solution ?



SOLUTION:



I managed to solve this issue using the following method:



void GenerateKeyCombination(int vk, int vk2, BOOL bExtended, BOOL bExtended2) {

KEYBDINPUT kb = {0};
INPUT Input = {0};
KEYBDINPUT kb2 = {0};
INPUT Input2 = {0};

// Generate a "key down" 1
if (bExtended) { kb.dwFlags = KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));

// Generate a "key down" 2
if (bExtended2) { kb2.dwFlags = KEYEVENTF_EXTENDEDKEY; }
kb2.wVk = vk2;
Input2.type = INPUT_KEYBOARD;
Input2.ki = kb2;
SendInput(1, &Input2, sizeof(Input2));

// Generate a "key up" 1
ZeroMemory(&kb, sizeof(KEYBDINPUT));
ZeroMemory(&Input, sizeof(INPUT));
kb.dwFlags = KEYEVENTF_KEYUP;
if (bExtended) { kb.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));

// Generate a "key up" 2
ZeroMemory(&kb2, sizeof(KEYBDINPUT));
ZeroMemory(&Input2, sizeof(INPUT));
kb2.dwFlags = KEYEVENTF_KEYUP;
if (bExtended2) { kb2.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
kb2.wVk = vk2;
Input2.type = INPUT_KEYBOARD;
Input2.ki = kb2;
SendInput(1, &Input2, sizeof(Input2));

return;
}


And calling it like so:



        GenerateKeyCombination(0x12, 0x44, FALSE, FALSE);


Where 0x12 is ALT and 0x44 is D.










share|improve this question

























  • I have no idea of winapi, but have you tried to provide two keydown events, and aftr that the key ups?

    – PlasmaHH
    May 10 '12 at 10:37











  • That is what I have been trying to do but Its just nor working for me at all. I have added a second method 'GenerateKeyCombination' that takes 4 arguments but Alt+D is still not working. It selects the settings icon in chrome instead of the address bar though.

    – L337BEAN
    May 10 '12 at 13:02


















0















I am trying to get my application to output a key combination ( ALT + D ) to focus on Internet explorers address bar, but I am having trouble implementing the code needed. I already have a method to pass 1 key:



void GenerateKey(int vk, BOOL bExtended) {

KEYBDINPUT kb = {0};
INPUT Input = {0};

/* Generate a "key down" */
if (bExtended) { kb.dwFlags = KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));

/* Generate a "key up" */
ZeroMemory(&kb, sizeof(KEYBDINPUT));
ZeroMemory(&Input, sizeof(INPUT));
kb.dwFlags = KEYEVENTF_KEYUP;
if (bExtended) { kb.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));

return;
}


Can anyone provide some help as to how to achieve a the desired solution ?



SOLUTION:



I managed to solve this issue using the following method:



void GenerateKeyCombination(int vk, int vk2, BOOL bExtended, BOOL bExtended2) {

KEYBDINPUT kb = {0};
INPUT Input = {0};
KEYBDINPUT kb2 = {0};
INPUT Input2 = {0};

// Generate a "key down" 1
if (bExtended) { kb.dwFlags = KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));

// Generate a "key down" 2
if (bExtended2) { kb2.dwFlags = KEYEVENTF_EXTENDEDKEY; }
kb2.wVk = vk2;
Input2.type = INPUT_KEYBOARD;
Input2.ki = kb2;
SendInput(1, &Input2, sizeof(Input2));

// Generate a "key up" 1
ZeroMemory(&kb, sizeof(KEYBDINPUT));
ZeroMemory(&Input, sizeof(INPUT));
kb.dwFlags = KEYEVENTF_KEYUP;
if (bExtended) { kb.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));

// Generate a "key up" 2
ZeroMemory(&kb2, sizeof(KEYBDINPUT));
ZeroMemory(&Input2, sizeof(INPUT));
kb2.dwFlags = KEYEVENTF_KEYUP;
if (bExtended2) { kb2.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
kb2.wVk = vk2;
Input2.type = INPUT_KEYBOARD;
Input2.ki = kb2;
SendInput(1, &Input2, sizeof(Input2));

return;
}


And calling it like so:



        GenerateKeyCombination(0x12, 0x44, FALSE, FALSE);


Where 0x12 is ALT and 0x44 is D.










share|improve this question

























  • I have no idea of winapi, but have you tried to provide two keydown events, and aftr that the key ups?

    – PlasmaHH
    May 10 '12 at 10:37











  • That is what I have been trying to do but Its just nor working for me at all. I have added a second method 'GenerateKeyCombination' that takes 4 arguments but Alt+D is still not working. It selects the settings icon in chrome instead of the address bar though.

    – L337BEAN
    May 10 '12 at 13:02














0












0








0








I am trying to get my application to output a key combination ( ALT + D ) to focus on Internet explorers address bar, but I am having trouble implementing the code needed. I already have a method to pass 1 key:



void GenerateKey(int vk, BOOL bExtended) {

KEYBDINPUT kb = {0};
INPUT Input = {0};

/* Generate a "key down" */
if (bExtended) { kb.dwFlags = KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));

/* Generate a "key up" */
ZeroMemory(&kb, sizeof(KEYBDINPUT));
ZeroMemory(&Input, sizeof(INPUT));
kb.dwFlags = KEYEVENTF_KEYUP;
if (bExtended) { kb.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));

return;
}


Can anyone provide some help as to how to achieve a the desired solution ?



SOLUTION:



I managed to solve this issue using the following method:



void GenerateKeyCombination(int vk, int vk2, BOOL bExtended, BOOL bExtended2) {

KEYBDINPUT kb = {0};
INPUT Input = {0};
KEYBDINPUT kb2 = {0};
INPUT Input2 = {0};

// Generate a "key down" 1
if (bExtended) { kb.dwFlags = KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));

// Generate a "key down" 2
if (bExtended2) { kb2.dwFlags = KEYEVENTF_EXTENDEDKEY; }
kb2.wVk = vk2;
Input2.type = INPUT_KEYBOARD;
Input2.ki = kb2;
SendInput(1, &Input2, sizeof(Input2));

// Generate a "key up" 1
ZeroMemory(&kb, sizeof(KEYBDINPUT));
ZeroMemory(&Input, sizeof(INPUT));
kb.dwFlags = KEYEVENTF_KEYUP;
if (bExtended) { kb.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));

// Generate a "key up" 2
ZeroMemory(&kb2, sizeof(KEYBDINPUT));
ZeroMemory(&Input2, sizeof(INPUT));
kb2.dwFlags = KEYEVENTF_KEYUP;
if (bExtended2) { kb2.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
kb2.wVk = vk2;
Input2.type = INPUT_KEYBOARD;
Input2.ki = kb2;
SendInput(1, &Input2, sizeof(Input2));

return;
}


And calling it like so:



        GenerateKeyCombination(0x12, 0x44, FALSE, FALSE);


Where 0x12 is ALT and 0x44 is D.










share|improve this question
















I am trying to get my application to output a key combination ( ALT + D ) to focus on Internet explorers address bar, but I am having trouble implementing the code needed. I already have a method to pass 1 key:



void GenerateKey(int vk, BOOL bExtended) {

KEYBDINPUT kb = {0};
INPUT Input = {0};

/* Generate a "key down" */
if (bExtended) { kb.dwFlags = KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));

/* Generate a "key up" */
ZeroMemory(&kb, sizeof(KEYBDINPUT));
ZeroMemory(&Input, sizeof(INPUT));
kb.dwFlags = KEYEVENTF_KEYUP;
if (bExtended) { kb.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));

return;
}


Can anyone provide some help as to how to achieve a the desired solution ?



SOLUTION:



I managed to solve this issue using the following method:



void GenerateKeyCombination(int vk, int vk2, BOOL bExtended, BOOL bExtended2) {

KEYBDINPUT kb = {0};
INPUT Input = {0};
KEYBDINPUT kb2 = {0};
INPUT Input2 = {0};

// Generate a "key down" 1
if (bExtended) { kb.dwFlags = KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));

// Generate a "key down" 2
if (bExtended2) { kb2.dwFlags = KEYEVENTF_EXTENDEDKEY; }
kb2.wVk = vk2;
Input2.type = INPUT_KEYBOARD;
Input2.ki = kb2;
SendInput(1, &Input2, sizeof(Input2));

// Generate a "key up" 1
ZeroMemory(&kb, sizeof(KEYBDINPUT));
ZeroMemory(&Input, sizeof(INPUT));
kb.dwFlags = KEYEVENTF_KEYUP;
if (bExtended) { kb.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));

// Generate a "key up" 2
ZeroMemory(&kb2, sizeof(KEYBDINPUT));
ZeroMemory(&Input2, sizeof(INPUT));
kb2.dwFlags = KEYEVENTF_KEYUP;
if (bExtended2) { kb2.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
kb2.wVk = vk2;
Input2.type = INPUT_KEYBOARD;
Input2.ki = kb2;
SendInput(1, &Input2, sizeof(Input2));

return;
}


And calling it like so:



        GenerateKeyCombination(0x12, 0x44, FALSE, FALSE);


Where 0x12 is ALT and 0x44 is D.







c++ winapi keypress






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 11 '12 at 11:01







L337BEAN

















asked May 10 '12 at 10:27









L337BEANL337BEAN

64461746




64461746













  • I have no idea of winapi, but have you tried to provide two keydown events, and aftr that the key ups?

    – PlasmaHH
    May 10 '12 at 10:37











  • That is what I have been trying to do but Its just nor working for me at all. I have added a second method 'GenerateKeyCombination' that takes 4 arguments but Alt+D is still not working. It selects the settings icon in chrome instead of the address bar though.

    – L337BEAN
    May 10 '12 at 13:02



















  • I have no idea of winapi, but have you tried to provide two keydown events, and aftr that the key ups?

    – PlasmaHH
    May 10 '12 at 10:37











  • That is what I have been trying to do but Its just nor working for me at all. I have added a second method 'GenerateKeyCombination' that takes 4 arguments but Alt+D is still not working. It selects the settings icon in chrome instead of the address bar though.

    – L337BEAN
    May 10 '12 at 13:02

















I have no idea of winapi, but have you tried to provide two keydown events, and aftr that the key ups?

– PlasmaHH
May 10 '12 at 10:37





I have no idea of winapi, but have you tried to provide two keydown events, and aftr that the key ups?

– PlasmaHH
May 10 '12 at 10:37













That is what I have been trying to do but Its just nor working for me at all. I have added a second method 'GenerateKeyCombination' that takes 4 arguments but Alt+D is still not working. It selects the settings icon in chrome instead of the address bar though.

– L337BEAN
May 10 '12 at 13:02





That is what I have been trying to do but Its just nor working for me at all. I have added a second method 'GenerateKeyCombination' that takes 4 arguments but Alt+D is still not working. It selects the settings icon in chrome instead of the address bar though.

– L337BEAN
May 10 '12 at 13:02












1 Answer
1






active

oldest

votes


















3














Add an accelerator map to your project resources,Load it into your application at runtime, and in your message loop add a call to TranslateAccelerator before TranslateMessage and DispatchMessage gets a chance to take a look at it.



http://msdn.microsoft.com/en-us/library/windows/desktop/ms646373%28v=vs.85%29.aspx for reference.






share|improve this answer


























  • I am marking this as the answer as this is probably the proper way of going about solving this issue. I however managed to solve it with the method I have added to my original question.

    – L337BEAN
    May 11 '12 at 10:59












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10532119%2fc-generating-key-combinations-winapi-without-mfc%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Add an accelerator map to your project resources,Load it into your application at runtime, and in your message loop add a call to TranslateAccelerator before TranslateMessage and DispatchMessage gets a chance to take a look at it.



http://msdn.microsoft.com/en-us/library/windows/desktop/ms646373%28v=vs.85%29.aspx for reference.






share|improve this answer


























  • I am marking this as the answer as this is probably the proper way of going about solving this issue. I however managed to solve it with the method I have added to my original question.

    – L337BEAN
    May 11 '12 at 10:59
















3














Add an accelerator map to your project resources,Load it into your application at runtime, and in your message loop add a call to TranslateAccelerator before TranslateMessage and DispatchMessage gets a chance to take a look at it.



http://msdn.microsoft.com/en-us/library/windows/desktop/ms646373%28v=vs.85%29.aspx for reference.






share|improve this answer


























  • I am marking this as the answer as this is probably the proper way of going about solving this issue. I however managed to solve it with the method I have added to my original question.

    – L337BEAN
    May 11 '12 at 10:59














3












3








3







Add an accelerator map to your project resources,Load it into your application at runtime, and in your message loop add a call to TranslateAccelerator before TranslateMessage and DispatchMessage gets a chance to take a look at it.



http://msdn.microsoft.com/en-us/library/windows/desktop/ms646373%28v=vs.85%29.aspx for reference.






share|improve this answer















Add an accelerator map to your project resources,Load it into your application at runtime, and in your message loop add a call to TranslateAccelerator before TranslateMessage and DispatchMessage gets a chance to take a look at it.



http://msdn.microsoft.com/en-us/library/windows/desktop/ms646373%28v=vs.85%29.aspx for reference.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 17 '18 at 0:19

























answered May 10 '12 at 22:05









johnathanjohnathan

2,178819




2,178819













  • I am marking this as the answer as this is probably the proper way of going about solving this issue. I however managed to solve it with the method I have added to my original question.

    – L337BEAN
    May 11 '12 at 10:59



















  • I am marking this as the answer as this is probably the proper way of going about solving this issue. I however managed to solve it with the method I have added to my original question.

    – L337BEAN
    May 11 '12 at 10:59

















I am marking this as the answer as this is probably the proper way of going about solving this issue. I however managed to solve it with the method I have added to my original question.

– L337BEAN
May 11 '12 at 10:59





I am marking this as the answer as this is probably the proper way of going about solving this issue. I however managed to solve it with the method I have added to my original question.

– L337BEAN
May 11 '12 at 10:59




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10532119%2fc-generating-key-combinations-winapi-without-mfc%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly