Get the Keyboard keycodes keys in Linux - c++

I need to get the keycodes from keyboard input (but after) in Linux, i been able to find
the keycodes in Windows only.
Using a enum i can make it cross-platform, heres is a piece (not complete, is too large) of
the enum code:
enum e_IWEKey
{
Unknown = 0,
// Digits / Numbers
D0,
D1,
D2,
D3,
D4,
D5,
D6,
D7,
D8,
D9,
// Letters
A,
B,
C,
D,
E,
F,
G,
H,
I,
J,
K,
L,
M,
N,
O,
P,
Q,
}
And here another piece the respective Win32API Keycodes.
#ifdef _WIN32
# include <WinUser.h>
inline e_IWEKey getKey(uchar OSKey)
{
switch (OSKey)
{
// Digits / Numbers
case 0x30: return IWEKeys::D0;
case 0x31: return IWEKeys::D1;
case 0x32: return IWEKeys::D2;
case 0x33: return IWEKeys::D3;
case 0x34: return IWEKeys::D4;
case 0x35: return IWEKeys::D5;
case 0x36: return IWEKeys::D6;
case 0x37: return IWEKeys::D7;
case 0x38: return IWEKeys::D8;
case 0x39: return IWEKeys::D9;
// Letters
case 0x41: return IWEKeys::A;
case 0x42: return IWEKeys::B;
case 0x43: return IWEKeys::C;
case 0x44: return IWEKeys::D;
case 0x45: return IWEKeys::E;
case 0x46: return IWEKeys::F;
case 0x47: return IWEKeys::G;
case 0x48: return IWEKeys::H;
case 0x49: return IWEKeys::I;
case 0x4A: return IWEKeys::J;
case 0x4B: return IWEKeys::K;
case 0x4C: return IWEKeys::L;
case 0x4D: return IWEKeys::M;
case 0x4E: return IWEKeys::N;
case 0x4F: return IWEKeys::O;
case 0x50: return IWEKeys::P;
case 0x51: return IWEKeys::Q;
case 0x52: return IWEKeys::R;
case 0x53: return IWEKeys::S;
case 0x54: return IWEKeys::T;
case 0x55: return IWEKeys::U;
case 0x56: return IWEKeys::V;
case 0x57: return IWEKeys::W;
case 0x58: return IWEKeys::X;
case 0x59: return IWEKeys::Y;
case 0x5A: return IWEKeys::Z;
// Function Keys
case VK_F1: return IWEKeys::F1;
case VK_F2: return IWEKeys::F2;
case VK_F3: return IWEKeys::F3;
case VK_F4: return IWEKeys::F4;
case VK_F5: return IWEKeys::F5;
case VK_F6: return IWEKeys::F6;
case VK_F7: return IWEKeys::F7;
case VK_F8: return IWEKeys::F8;
case VK_F9: return IWEKeys::F9;
case VK_F10: return IWEKeys::F10;
case VK_F11: return IWEKeys::F11;
case VK_F12: return IWEKeys::F12;
case VK_F13: return IWEKeys::F13;
case VK_F14: return IWEKeys::F14;
case VK_F15: return IWEKeys::F15;
case VK_F16: return IWEKeys::F16;
case VK_F17: return IWEKeys::F17;
case VK_F18: return IWEKeys::F18;
case VK_F19: return IWEKeys::F19;
case VK_F20: return IWEKeys::F20;
case VK_F21: return IWEKeys::F21;
case VK_F22: return IWEKeys::F22;
case VK_F23: return IWEKeys::F23;
case VK_F24: return IWEKeys::F24;
}
}
#endif
but i never find the Linux keycodes, i need to use the internal Linux API (or POSIX), the X11 API or toolkit-specific API? (as Qt).
Thanks.

If you can use qt, there is Qt::Key enum.
Base on how keys are defined in qt, I think key codes would not change between linux and windows...

Related

How to return QVariant type array

I am recently creating a model for qml with c++, but I face a problem when returning a QVariant type empty array. How should I define my return statement?
switch (role) {
case NameRole:
return QVariant(QStringLiteral("AAAAA"));
case LevelRole:
return QVariant(QStringLiteral("1"));
case ParentRole:
return QVariant(QStringLiteral("null"));
case SublevelRole:
return ???// I would like to return an empty array
}
Use QVariantList:
switch (role) {
case NameRole:
return QVariant(QStringLiteral("AAAAA"));
case LevelRole:
return QVariant(QStringLiteral("1"));
case ParentRole:
return QVariant(QStringLiteral("null"));
case SublevelRole:
return QVariantList();
}

Capping the value

I have a small script that defines the casting time for all classes in this minor project im working on however i have a few issues.
I want to place a cap on the max value however i'm getting errors!
this is the function i was referring to.
void Player::ApplyRatingMod(CombatRating combatRating, int32 value, bool apply)
{
float oldRating = m_baseRatingValue[combatRating];
m_baseRatingValue[combatRating] += (apply ? value : -value);
// explicit affected values
float const multiplier = GetRatingMultiplier(combatRating);
float const oldVal = oldRating * multiplier;
float const newVal = m_baseRatingValue[combatRating] * multiplier;
switch (combatRating)
{
case CR_HASTE_MELEE:
ApplyAttackTimePercentMod(BASE_ATTACK, oldVal, false);
ApplyAttackTimePercentMod(OFF_ATTACK, oldVal, false);
ApplyAttackTimePercentMod(BASE_ATTACK, newVal, true);
ApplyAttackTimePercentMod(OFF_ATTACK, newVal, true);
break;
case CR_HASTE_RANGED:
ApplyAttackTimePercentMod(RANGED_ATTACK, oldVal, false);
ApplyAttackTimePercentMod(RANGED_ATTACK, newVal, true);
break;
case CR_HASTE_SPELL:
//ApplyCastTimePercentMod(oldVal, false);
//ApplyCastTimePercentMod(newVal, true);
break;
default:
break;
}
UpdateRating(combatRating);
}
void Player::UpdateRating(CombatRating cr)
{
int32 amount = m_baseRatingValue[cr];
// Apply bonus from SPELL_AURA_MOD_RATING_FROM_STAT
// stat used stored in miscValueB for this aura
AuraEffectList const& modRatingFromStat = GetAuraEffectsByType(SPELL_AURA_MOD_RATING_FROM_STAT);
for (AuraEffect const* aurEff : modRatingFromStat)
if (aurEff->GetMiscValue() & (1 << cr))
amount += int32(CalculatePct(GetStat(Stats(aurEff->GetMiscValueB())), aurEff->GetAmount()));
if (amount < 0)
amount = 0;
SetUInt32Value(PLAYER_FIELD_COMBAT_RATING_1 + cr, uint32(amount));
bool affectStats = CanModifyStats();
switch (cr)
{
case CR_WEAPON_SKILL: // Implemented in Unit::RollMeleeOutcomeAgainst
case CR_DEFENSE_SKILL:
UpdateDefenseBonusesMod();
break;
case CR_DODGE:
UpdateDodgePercentage();
break;
case CR_PARRY:
UpdateParryPercentage();
break;
case CR_BLOCK:
UpdateBlockPercentage();
break;
case CR_HIT_MELEE:
UpdateMeleeHitChances();
break;
case CR_HIT_RANGED:
UpdateRangedHitChances();
break;
case CR_HIT_SPELL:
UpdateSpellHitChances();
break;
case CR_CRIT_MELEE:
if (affectStats)
{
UpdateCritPercentage(BASE_ATTACK);
UpdateCritPercentage(OFF_ATTACK);
}
break;
case CR_CRIT_RANGED:
if (affectStats)
UpdateCritPercentage(RANGED_ATTACK);
break;
case CR_CRIT_SPELL:
if (affectStats)
UpdateAllSpellCritChances();
break;
case CR_HIT_TAKEN_MELEE: // Implemented in Unit::MeleeMissChanceCalc
case CR_HIT_TAKEN_RANGED:
break;
case CR_HIT_TAKEN_SPELL: // Implemented in Unit::MagicSpellHitResult
break;
case CR_CRIT_TAKEN_MELEE: // Implemented in Unit::RollMeleeOutcomeAgainst (only for chance to crit)
case CR_CRIT_TAKEN_RANGED:
break;
case CR_CRIT_TAKEN_SPELL: // Implemented in Unit::SpellCriticalBonus (only for chance to crit)
break;
case CR_HASTE_MELEE: // Implemented in Player::ApplyRatingMod
case CR_HASTE_RANGED:
case CR_HASTE_SPELL:
break;
case CR_WEAPON_SKILL_MAINHAND: // Implemented in Unit::RollMeleeOutcomeAgainst
case CR_WEAPON_SKILL_OFFHAND:
case CR_WEAPON_SKILL_RANGED:
break;
case CR_EXPERTISE:
if (affectStats)
{
UpdateExpertise(BASE_ATTACK);
UpdateExpertise(OFF_ATTACK);
}
break;
case CR_ARMOR_PENETRATION:
if (affectStats)
UpdateArmorPenetration(amount);
break;
}
}
void Player::UpdateAllRatings()
{
for (uint8 cr = 0; cr < MAX_COMBAT_RATING; ++cr)
UpdateRating(CombatRating(cr));
}
You can notice how i want to cap the new value outcome to 32000.
Before it gets calculated to percentages!
I tried using
if(newVal > 32000)
newVal = 32000;
Which would normally do the job i guess, but i'm stuck with an error
E0137 expression must be a modifiable lvalue
On the line newVal = 32000;
Remove const qualifier from newVal
float newVal = m_baseRatingValue[combatRating] * multiplier;
You declare newVal as a constant (float const newVal = ...), so you're not allowed to assign a new value.
Removing const should work for you, e.g.
float newVal = m_baseRatingValue[combatRating] * multiplier;
if (newVal > 32000)
newVal = 32000;
...

C++ Forced to do a weird cast to get rid of "expression should be a modifiable lvalue" [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I just faced a strange enum assignation problem and I thought you could have helped me out. I have an enum like this:
enum LIB_EDXENGINE CameraFeatureDataType
{
CFDT_ENUMERATION,
CFDT_64BITS_UINT,
CFDT_64BITS_INT,
CFDT_64BITS_FLOAT,
CFDT_BOOLEAN,
CFDT_32BITS_UINT,
CFDT_32BITS_INT,
CFDT_32BITS_FLOAT,
};
And I have a class with an instance of this enum.
EDITED:
const CameraFeatureDataType & LibEDX::CVBGenicamFeature2::GetFeatureType() const
{
switch (m_eNodeType)
{
case NT_Boolean:
m_eCameraFeatureDataType = CFDT_BOOLEAN;
break;
case NT_Integer:
m_eCameraFeatureDataType = CFDT_64BITS_INT;
break;
case NT_Float:
m_eCameraFeatureDataType = CFDT_64BITS_FLOAT;
break;
case NT_Enumeration:
m_eCameraFeatureDataType = CFDT_ENUMERATION;
break;
}
return m_eCameraFeatureDataType;
}
The only solution that I found to get rid of the error is to cast the member variable to its own type, which is weird I think.
EDITED TOO:
const CameraFeatureDataType & LibEDX::CVBGenicamFeature2::GetFeatureType() const
{
switch (m_eNodeType)
{
case NT_Boolean:
(CameraFeatureDataType)m_eCameraFeatureDataType = CFDT_BOOLEAN;
break;
case NT_Integer:
(CameraFeatureDataType)m_eCameraFeatureDataType = CFDT_64BITS_INT;
break;
case NT_Float:
(CameraFeatureDataType)m_eCameraFeatureDataType = CFDT_64BITS_FLOAT;
break;
case NT_Enumeration:
(CameraFeatureDataType)m_eCameraFeatureDataType = CFDT_ENUMERATION;
break;
}
return m_eCameraFeatureDataType;
}
You declared method as const and then you do member attribute modification:
const CameraFeatureDataType & LibEDX::CVBGenicamFeature2::GetFeatureType() const
{
switch (m_eNodeType)
{
case NT_Boolean:
this->m_eCameraFeatureDataType = CFDT_BOOLEAN;
break;
case NT_Integer:
this->m_eCameraFeatureDataType = CFDT_64BITS_INT;
break;
case NT_Float:
this->m_eCameraFeatureDataType = CFDT_64BITS_FLOAT;
break;
case NT_Enumeration:
this->m_eCameraFeatureDataType = CFDT_ENUMERATION;
break;
}
return this->m_eCameraFeatureDataType;
}
You propably should write:
const CameraFeatureDataType & LibEDX::CVBGenicamFeature2::GetFeatureType() const
{
switch (m_eNodeType)
{
case NT_Boolean:
return CFDT_BOOLEAN;
case NT_Integer:
return CFDT_64BITS_INT;
case NT_Float:
return CFDT_64BITS_FLOAT;
case NT_Enumeration:
return CFDT_ENUMERATION;
}
return CFDT_UNKNOWN;//change to default value
}
Your problem is that you are trying to modify a member variable in a const method.
This is what your method would look like normally:
CameraFeatureDataType LibEDX::CVBGenicamFeature2::GetFeatureType() const
{
switch (m_eNodeType)
{
case NT_Boolean:
return CFDT_BOOLEAN;
case NT_Integer:
return CFDT_64BITS_INT;
case NT_Float:
return CFDT_64BITS_FLOAT;
case NT_Enumeration:
return CFDT_ENUMERATION;
default:
// don't know either, you decide...
}
}
I have no idea why you even have a member variable, or why you would return an enum value as a const reference. If you need one of those, you should think hard about why you need them. The keyword for changing a member variable in a constmethod is mutable.

Switch case Statement in Python. Instead of using if else statement is there any other way

Below is the code in javascript. I need to convert it into Python. Having a multiple switch cases with one return value.:
textOfBetType: function(tip) {
switch (tip) {
case "tip1":
case "tip0":
case "tip2":
return "Livewette 0:0"
//return "Sieger (3-Weg)"
case "tipOver":
case "tipUnder":
return "Über/Unter 2,5";
}
return null;
}
I used "if and else" statement in Python.
def text_of_bet_type(self, tip):
if tip == "tip1" or tip == "tip0" or tip == "tip2":
return "Livewette 0:0"
else:
return 'null';
but is there any other way to do this this..

Simplyfing long code lines

This code is to list a cards deck in string (Kh,6c,5h, etc..) from a int (0 from 51) or vice versa.
I have written code for it but it seems very long. Is there a more efficient way to write this ?
I want to do this both way too, send a string to a function and get an int.
std::string Card::getString(int card) {
std::string cardstring;
switch (card) {
case 0:
return "2c";
case 1:
return "3c";
case 2:
return "4c";
case 3:
return "5c";
case 4:
return "6c";
case 5:
return "7c";
case 6:
return "8c";
case 7:
return "9c";
case 8:
return "Tc";
case 9:
return "Jc";
case 10:
return "Qc";
case 11:
return "Kc";
case 12:
return "Ac";
case 13:
return "2d";
case 14:
return "3d";
case 15:
return "4d";
case 16:
return "5d";
case 17:
return "6d";
case 18:
return "7d";
case 19:
return "8d";
case 20:
return "9d";
case 21:
return "Td";
case 22:
return "Jd";
case 23:
return "Qd";
case 24:
return "Kd";
case 25:
return "Ad";
case 26:
return "2h";
case 27:
return "3h";
case 28:
return "4h";
case 29:
return "5h";
case 30:
return "6h";
case 31:
return "7h";
case 32:
return "8h";
case 33:
return "9h";
case 34:
return "Th";
case 35:
return "Jh";
case 36:
return "Qh";
case 37:
return "Kh";
case 38:
return "Ah";
case 39:
return "2s";
case 40:
return "3s";
case 41:
return "4s";
case 42:
return "5s";
case 43:
return "6s";
case 44:
return "7s";
case 45:
return "8s";
case 46:
return "9s";
case 47:
return "Ts";
case 48:
return "Js";
case 49:
return "Qs";
case 50:
return "Ks";
case 51:
return "As";
}
return cardstring;}
thanks
std::string get_card_string(int card)
{
if (card >= 0 && card < 52)
{
std::string s(2,' ');
s[0] = "23456789TJQKA"[card % 13];
s[1] = "cdhs"[card / 13];
return s;
}
return "";
}
The reverse process is a little more complicated. If I thought about it for a while, I might come up with a more clever method, but the obvious choice would be something like this:
std::unordered_map<std::string, int> initialize_card_map()
{
std::unordered_map<std::string, int> m;
for (int i=0; i<52; ++i)
m[get_card_string(i)] = i;
return m;
}
int get_card_number(std::string const & card_string)
{
static std::unordered_map<std::string, int> const m = initialize_card_map();
auto it = m.find(card_string);
if (it != m.end())
return it->second;
return ??? value not found
}
Use an std::array or an std::vector:
std::vector<std::string> cards{
"2c", // index 0
"3c", // index 1
"4c"...
};
std::string Card::getString(int card) { return cards[card]; }
assert(getString(0) == "2c");
Benjamin Lindley`s above answer is great, but if you are writing code like the original post in the first place you need to be asking youself more questions about your design choices:
Why do you have to access the card value via an int?
How about a tuple to represent card values?
Peter Norvig's "Design of Computer Programs" course on Udacity.com's first lesson/lecture seems like it would extremely relevant to you. I suggest taking a look at it.