Is scan code the same on all keyboards - layout

I got problem when one of my customer complains about they use azerty keyboard and they can't use my product. So I decide to use scan code instead of virtual key. I found the function MapVirtualKey is really useful for me to achieve that. But in some situation, I don't want to use MapVirtualKey function but use the number itself, like {if(isKeyDown[30])return 'A';} but I go around the internet and realize that some source tell that their keyboard scan code is not like mine, like in this image
I don't understand it, why it's different from my keyboard's scan code, and even different from this MS scan code table
So I really wonder, Is it safe to use
if (isDownNow[48])
return 'B';
instead of
if (isDownNow[MapVirtualKey('B', MAPVK_VK_TO_VSC)])
return 'B';
Thank you for reading :)
Edit:
I think I have a solution for the problem above, instead of call MapVirtualKey every time, I will store the map in an array. But new problem comes up.
I don't have a azerty keyboard so I can't test this, I only have a qwerty keyboard, so I got confuse on this problem:
I got this function to store my map.
void MapKeyData()
{
for (int i = 0; i < KEYS_SIZE; i++)//KEYS_SIZE=255
mapKey[i] = MapVirtualKey(i, MAPVK_VK_TO_VSC);//mapKey is unsigned char array
}
But I want the Z in normal keyboard (qwerty keyboard) map with the W in azerty keyboard. But as the function MapKeyData above, I think the Z in qwerty keyboard still map with the Z in azerty keyboard which is definitely not my purpose, I want to keep the keyboard layout, not the key itself. But as I said, I don't know if the scan code is the same on every keyboard as the first picture show that the keycode different from my scan code.
Thank for reading :)

Yes, scan code is the same on all keyboard layout. I've tested this fact by changing my keyboard layout to other layout with windows settings.
So to solve the different keyboard layout across PCs, I created an array of unsigned char that stores the scan code of keys: The index of the item is the key on my standard QWERTY keyboard, the value of the item is the scan code. This way, I can easily map the key on my QWERTY to its scan code so that I can work with the scan code.
mapKey['A'] = 30; //Key 'A' on QWERTY keyboard has scan code = 30
mapKey['B'] = 48;
mapKey['C'] = 46;
mapKey['D'] = 32;
mapKey['E'] = 18;
mapKey['F'] = 33;
mapKey['G'] = 34;
mapKey['H'] = 35;
mapKey['I'] = 23;
mapKey['J'] = 36;
mapKey['K'] = 37;
mapKey['L'] = 38;
mapKey['M'] = 50;
mapKey['N'] = 49;
mapKey['O'] = 24;
mapKey['P'] = 25;
mapKey['Q'] = 16;
mapKey['R'] = 19;
mapKey['S'] = 31;
mapKey['T'] = 20;
mapKey['U'] = 22;
mapKey['V'] = 47;
mapKey['W'] = 17;
mapKey['X'] = 45;
mapKey['Y'] = 21;
mapKey['Z'] = 44;
mapKey[VK_LEFT] = 75;
mapKey[VK_RIGHT] = 77;
mapKey[VK_RETURN] = 28;
... more if needed

See my answer here -- tl;dr: use WM_CHAR, not MapVirtualKey

Related

How in Scriptui (Extendscript) can I use subscripts or superscripts in button title?

How in Scriptui (Extendscript) can I use subscripts or superscripts in button title? I'm using buttonTitle as an example for any character string that is displayed on the dialog/palette.
In this example:
var win = new Window("dialog");
var buttonTitle = "Button2";
win.aButton = win.add("button", undefined, buttonTitle);
win.show();
how do I code buttonTitle so it is "ButtonX" where X is either a superscript or subscript 2? Or maybe letter Y? Numbers are available in some fonts but letters probably aren't. I would like a general solution.
I appreciate your time.
Thanks,
RONC
This is a screen plot of scriptui problem:
RONC
Just saw this today, so sorry it took so long.
You can try using unicode codes for this. See the tables on this page:
https://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts
But those characters sure are tiny! There are codes for letter characters available, but I didn't test any. Here's some code to test, with notes:
function buildUI(this_obj_) {
var win = (this_obj_ instanceof Panel)
? this_obj_
: new Window('palette', 'Script Window',[147,196,528,330]);
// \u00B2 for super 2, \u00B3 for super 3; the rest are \u2070 for super zero, \u2071 for super one, \u2074 for super four, \u2075 for super five, etc...
win.xui_ui_button5 = win.add('button', [49,24,177,54], 'Super\u00B2');
win.xui_ui_button5.onClick = function () {this.parent.close(1);}
// \u2081 for sub 1, \u2082 for sub 2, \u2083 for sub 3, etc...
win.xui_ui_button6 = win.add('button', [49,65,177,95], 'Sub\u2082');
win.xui_ui_button6.onClick = function () {this.parent.close(1);}
return win
}
var w = buildUI(this);
if (w.toString() == "[object Panel]") {
w;
} else {
w.show();
}
[EDIT: Here's a screenshot of the palette window this code works to create in After Effects]
[EDIT 2: Here's code that works in Photoshop using a modal dialog (and I'm testing on an old version of PS on this machine, but it should work, but look different on the latest]
var win = new Window('dialog', 'Script Window',[670,456,1160,598]);
var w = buildUI();
if (w != null) {
w.show();
}
function buildUI() {
if (win != null) {
win.xui_ui_button5 = win.add('button', [64,11,231,51], 'Super\u00B2');
win.xui_ui_button5.onClick = function () {this.parent.close(1);}
win.xui_ui_button6 = win.add('button', [264,63,431,103], 'Sub\u2082');
win.xui_ui_button6.onClick = function () {this.parent.close(1);}
}
return win
}
Here's super x, y and z working in PS, using codes \u02E3, \u02B8 and \u1DBB:

object array positioning-LibGdx

In my game,if I touch a particular object,coin objects will come out of them at random speeds and occupy random positions.
public void update(delta){
if(isTouched()&& getY()<Constants.WORLD_HEIGHT/2){
setY(getY()+(randomSpeed * delta));
setX(getX()-(randomSpeed/4 * delta));
}
}
Now I want to make this coins occupy positions in some patterns.Like if 3 coins come out,a triangle pattern or if 4 coins, rectangular pattern like that.
I tried to make it work,but coins are coming out and moved,but overlapping each other.Not able to create any patterns.
patterns like:
This is what I tried
int a = Math.abs(rndNo.nextInt() % 3)+1;//no of coins
int no =0;
float coinxPos = player.getX()-coins[0].getWidth()/2;
float coinyPos = player.getY();
int minCoinGap=20;
switch (a) {
case 1:
for (int i = 0; i < coins.length; i++) {
if (!coins[i].isCoinVisible() && no < a) {
coins[i].setCoinVisible(true);
coinxPos = coinxPos+rndNo.nextInt()%70;
coinyPos = coinyPos+rndNo.nextInt()%70;
coins[i].setPosition(coinxPos, coinyPos);
no++;
}
}
break;
case 2:
for (int i = 0; i < coins.length; i++) {
if (!coins[i].isCoinVisible() && no < a) {
coins[i].setCoinVisible(true);
coinxPos = coinxPos+minCoinGap+rndNo.nextInt()%70;
coinyPos = coinyPos+rndNo.nextInt()%150;
coins[i].setPosition(coinxPos, coinyPos);
no++;
}
}
break:
......
......
default:
break;
may be this is a simple logic to implement,but I wasted a lot of time on it and got confused of how to make it work.
Any help would be appreciated.
In my game, when I want some object at X,Y to reach some specific coordinates Xe,Ye at every frame I'm adding to it's coordinates difference between current and wanted position, divided by constant and multiplied by time passed from last frame. That way it starts moving quickly and goes slowly and slowly as it's closer, looks kinda cool.
X += ((Xe - X)* dt)/ CONST;
Y += ((Ye - Y)* dt)/ CONST;
You'll experimentally get that CONST value, bigger value means slower movement. If you want it to look even cooler you can add velocity variable and instead of changing directly coordinates depending on distance from end position you can adjust that velocity. That way even if object at some point reaches the end position it will still have some velocity and it will keep moving - it will have inertia. A bit more complex to achieve, but movement would be even wilder.
And if you want that Xe,Ye be some specific position (not random), then just set those constant values. No need to make it more complicated then that. Set like another constat OFFSET:
static final int OFFSET = 100;
Xe1 = X - OFFSET; // for first coin
Ye1 = Y - OFFSET;
Xe2 = X + OFFSET; // for second coin
Ye2 = Y - OFFSET;
...

Assigning a new string crashes?

I'm trying to write my first real program with dynamic arrays, but I've come across a problem I cannot understand. Basically, I am trying to take a dynamic array, copy it into a temporary one, add one more address to the original array, then copy everything back to the original array. Now the original array has one more address than before. This worked perfectly when trying with ints, but strings crash my program. Here's an example of the code I'm struggling with:
void main()
{
int x = 3;
std::string *q;
q = new std::string[x];
q[0] = "1";
q[1] = "2";
q[2] = "3";
x++;
std::string *temp = q;
q = new std::string[x];
q = temp;
q[x-1] = "4";
for (int i = 0; i < 5; i++)
std::cout << q[i] << std::endl;
}
If I were to make q and temp into pointers to int instead of string then the program runs just fine. Any help would be greatly appreciated, I've been stuck on this for an hour or two.
q = temp performs only a shallow copy. You lose the original q and all of the strings it pointed to.
Since you reallocated q to have 4 elements, but then immediately reassigned temp (which was allocated with only 3 elements), accessing (and assigning) the element at x now is outside the bounds of the array.
If you have to do it this way for some reason, it should look like this:
auto temp = q;
q = new std::string[x];
for(int x = 0; x < 3; ++x)
q[x] = temp[x];
delete [] temp;
q[x] = 4;
However, this is obviously more complex and very much more prone to error than the idiomatic way of doing this in C++. Better to use std::vector<std::string> instead.

Fade In & Out with Haxe & OpenFL

I am using a For Statement clock with Haxe and OpenFL, to create a Fade In & Fade Out effect for a Bitmap object (using alpha property). Do you have use other methods for that?
I am using a For statement instead of Sys.sleep(0.2); because it makes a "Loading" cursor icon and the program gets stuck.
The bitmap is called bmp and the timer is tmrSplash.
Source:
tmrSplash.run = function changeAlpha(): Void {
var f: Float = 1;
while (f <= 1 && f >= 0) {
bmp.alpha = f;
var a: Int = 0;
while (a < 500000000) {
a++;
if (a >= 500000000) {
a = 0;
}
f -= 0.0005;
trace(f);
}
tmrSplash.stop();
}
}
Thank you.
In order to fade out manually, you'll need to listen to the ENTER_FRAME event on the stage and change the value over time, instead of creating this infinite loop.
But I would suggest to take a look into a tween engine, like Actuate, which simplifies the syntax. You dont want to create something like this yourself, just use a library to animate properties. https://github.com/openfl/actuate
Actuate.tween (mySprite, 1, { alpha: 0 }); // fade out
Life is too short to invent another tween engine, dont waste your time on building such.

Use XGetPointerMapping/XSetPointerMapping to swap mouse buttons

I have found these two APIs at http://linux.die.net/man/3/xgetpointermapping. I think that they can be used to swap mouse buttons on linux or mac system. I used it in following way:
Display * curdisp; // Current display.
char curmap[MAX_NUM];// Current mapping.
int nmap; // number of mappings.
curdisp = XOpenDisplay(NULL);
nmap = XGetPointerMapping(curdisp, curmap, MAX_NUM);
if(!nmap)
return -1;
if(curmap[0] == '1' && curmap[2] == '3') {
curmap[0] = '3';
curmap[2] = '1';
} else {
curmap[0] = '1';
curmap[2] = '3';
}
//Set the mapping.
nmap = XSetPointerMapping(curdisp, curmap, nmap);
But the call XSetPointerMapping returns 0 and there is no effect on mouse button.
Can anyone give some example of swapping mouse button using XSetPointerMapping? or How to use it correctly? Will it work immediately?
The OS being used is Mac OS X 10.7.4.
The button numbers are stored as unsigned char but are not stored as characters.
Change '1' and '3' to 1 and 3.
Your code maps them to buttons 49 and 51 and does affect the buttons making button 1 and 3 unusable.

Resources