How about reading the documentation?
StringGetCharacter - https://docs.mql4.com/strings/stringgetcharacter
How to convert a single char string to keycode?
For example for the string "q" I want to get the value 81 (as the keycode described here).
hello,
in case you want a single char, you should enclose it in single quotes, e.g. char c='b'; and not double quotes "b". That way char will have the value you are after and you can use it in ifs or where ever, for example
char c='b'; if (c==98) Alert("all right");
int q = 'q'; // Not "q" Character Constants - Integer Types - Data Types - Language Basics - MQL4 Reference
@FMIC, I already did that, but StringGetChar("q", 0) returns 113 (ASCII code) instead of 81 (key code).
@Demos and WHRoeder, a single char or int works, but I want that the user of my indicator to enter the character and if I use
input char myChar = 'q';
then the user is presented with an integer input instead of string/character input.
Issue is still open.
One possible solution is to write a function myself that converts a single character string to the key code, but isn't there an API or existing function for that?
Well, it seems I misinterpreted your request, thinking that you wanted either the ASCII Code or UNICODE Code. However, in MQL4, there is no function for Virtual Key Codes or Keyboard codes like in Adobe Flash, and you will have to use Win32 functions for that (e.g. VkKeyScanEx).
However, I cannot see why you would want to convert characters in strings into Key Codes in MetaTrader. Maybe, if you explain your requirement we can provide you with a less complicated solution to your "real" problem.
nikolaygmt: then the user is presented with an integer input instead of string/character input. Issue is still open. One possible solution is to write a function myself that converts a single character string to the key code, but isn't there an API or existing function for that? |
|
I have been using something like this to add a keyChar to the keyboard buffer. However, the special keys are not included.
string uslayout = "00000409"; int h = LoadKeyboardLayoutW(uslayout, 0x00000001); uchar vkey = (uchar)VkKeyScanExW(keyChar, h); uchar skey = (uchar)MapVirtualKeyW(vkey, 0); keybd_event(vkey,skey,0 , 0); keybd_event(vkey,skey, KEYEVENTF_KEYUP,0);
@FMIC, I already did that, but StringGetChar("q", 0) returns 113 (ASCII code) instead of 81 (key code).
@Demos and WHRoeder, a single char or int works, but I want that the user of my indicator to enter the character and if I use
input char myChar = 'q';
then the user is presented with an integer input instead of string/character input.
Issue is still open.
One possible solution is to write a function myself that converts a single character string to the key code, but isn't there an API or existing function for that?
Use function StringToUpper()
Key code q=Q
@Tecuciztecatl, thank you! Yes StringToUpper() did the trick, as I didn't have to differentiate between lower or uppercase and the inputs had to be only alphabetical or single digit.
int getKeyCode(string val) {
StringToUpper(val);
return StringGetChar(val, 0);
}
@FMIC, @WHRoeder, thank you!
@Ovo, exactly what I was searching as a win32 solution. I do not know how to import the win32 functions correctly. Can you tell me (or share an article) how to do correctly imports in mq4 for win32 functions, like LoadKeyboardLayout, VkKeyScanEx, etc.
@Tecuciztecatl, thank you! Yes StringToUpper() did the trick, as I didn't have to differentiate between lower or uppercase and the inputs had to be only alphabetical or single digit.
int getKeyCode(string val) {
StringToUpper(val);
return StringGetChar(val, 0);
}
@FMIC, @WHRoeder, thank you!
@Ovo, exactly what I was searching as a win32 solution. I do not know how to import the win32 functions correctly. Can you tell me (or share an article) how to do correctly imports in mq4 for win32 functions, like LoadKeyboardLayout, VkKeyScanEx, etc.
The article:
https://docs.mql4.com/basis/preprosessor/import
Below is an example, but I am mapping types for an easier import.
#include <Ovo/imports/constants.import.mqh> #import "user32.dll" /** For codes see http://www.codeproject.com/Articles/7305/Keyboard-Events-Simulation-using-keybd-event-funct */ #define VK_F4 0x73 // F4 key #define VK_MENU 0x12 // ALT key VOID WINAPI keybd_event( _In_ BYTE bVk, _In_ BYTE bScan, _In_ DWORD dwFlags, _In_ ULONG_PTR dwExtraInfo ); HKL WINAPI LoadKeyboardLayoutW( _In_ LPCTSTR pwszKLID, _In_ UINT Flags ); SHORT WINAPI VkKeyScanExW( _In_ TCHAR ch, _In_ HKL dwhkl ); UINT WINAPI MapVirtualKeyW( _In_ UINT uCode, _In_ UINT uMapType ); HKL WINAPI GetKeyboardLayout( _In_ DWORD idThread ); HKL WINAPI ActivateKeyboardLayout( _In_ HKL hkl, _In_ UINT Flags ); #define KEYEVENTF_EXTENDEDKEY 0x0001 #define KEYEVENTF_KEYUP 0x0002 #define KEYEVENTF_UNICODE 0x0004 #define VK_HOME 0x24 #define VK_TAB 0x09 // 0F, 8F #define VK_RETURN 0x0D #import
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
How to convert a single char string to keycode?
For example for the string "q" I want to get the value 81 (as the keycode described here).