Bug on key up events in allegro - c

I'm doing a homework of my course using Allegro, as ordered.
I need to catch the key up events on keyboard, for this I used the follow code:
al_wait_for_event(evento, &ev);
if(ev.type == ALLEGRO_EVENT_KEY_UP){
if (ev.keyboard.keycode==ALLEGRO_KEY_A || ev.keyboard.keycode==ALLEGRO_KEY_LEFT)
cmd='a';
else if (ev.keyboard.keycode==ALLEGRO_KEY_W || ev.keyboard.keycode==ALLEGRO_KEY_UP)
cmd='w';
else if (ev.keyboard.keycode==ALLEGRO_KEY_D || ev.keyboard.keycode==ALLEGRO_KEY_RIGHT)
cmd='d';
else if (ev.keyboard.keycode==ALLEGRO_KEY_S || ev.keyboard.keycode==ALLEGRO_KEY_DOWN)
cmd='s';
}
But this event return the value 6 times​​ at once!
On the code I have included (previously) this commands:
al_install_keyboard();
[...]
al_register_event_source(evento, al_get_keyboard_event_source());
Where am I wrong?

Before the ALLEGRO_EVENT_KEY_UP event, there will be the ALLEGRO_EVENT_KEY_DOWN.
Just change UP to DOWN and it'll do.

Related

Lost on how to implement writeCharecteristic in my android code

I am using android studio's example bluetoothlegatt, which has a read function and and works completely find (it interacts with the TI keyfob I have which sends over values when pressed).
However, I have no idea how to put in writeCharecteristic because my previous attempts have failed.
Can someone help me with this? I would like to send a "1" over so that the keyfob's led lights up.
Edit: I have tried something like this
public void writeCharacteristic(byte[] value) { BluetoothGattService LumService = mBluetoothGatt.getService(UUID_LUM_APP);
if (LumService == null) {
System.out.println("LumService null"); return; }
BluetoothGattCharacteristic LumChar = LumService.getCharacteristic(UUID_LUM_CHAR);
if (LumChar == null) {
System.out.println("LumChar null"); return; }
LumChar.setValue(value); boolean status = mBluetoothGatt.writeCharacteristic(LumChar);
System.out.println("Write Status: " + status);
This is not my code, though, just an example. I want to integrate this into the bluetoothlegatt given already, but I have had no success with it.

how to check the radio button is checked or not in windows form

I have 3 pair of radiobutton at my form I want to check at every group of radiobutton one radiobutton is checked when user press save button. I used the below code to check every radiobutton, but I think this one a better approach to check.
if (radioBttnAddrssCoYes.Checked || rdoBttnAddCorrectNo.Checked)
{
if (rdoGroundFloorTrue.Checked || radioGroundFFalse.Checked)
{
if (rdoHomeTrue.Checked || radioMeet.Checked)
{
// Here I do something
}
else
{
MessageBox.Show("Please check Customer All day At Home");
}
}
else
{
MessageBox.Show("Please Check Ground Floor Delivery");
}
}
else
{
MessageBox.Show("Check Addresss Correct");
}
Thanks For your support and help
I would probably do it a little different, but it doesn't mean your way is wrong.
if(VaildateRadioGroupIsChecked(radioBttnAddrssCoYes, rdoBttnAddCorrectNo, "Check Addresss Correct") &&
VaildateRadioGroupIsChecked(rdoGroundFloorTrue, radioGroundFFalse, "Please Check Ground Floor Delivery") &&
VaildateRadioGroupIsChecked(rdoHomeTrue, radioMeet, "Please check Customer All day At Home"))
{
// do your thing...
}
private bool VaildateRadioGroupIsChecked(RadioButton a, RadioButton b, string MessageToUser)
{
if(!a.Checked && !b.Checked) {
Messagebox.Show(MessageToUser);
return false;
}
return true;
}
Of course, you can develop it further and not send the 2 radio buttons to the function, but it's containing element. this way, if you have 3, 4, or n radio buttons you can check all of them in one simple call.
Another well known technique to validate that a radio box is checked is to simply hold a boolean variable on the form level that is false by default, and changed to true when any of the radio buttons is checked. However, this technique will not serve you well in this case, since you will need 3 boolean variables for that, and the code will be unnecessary complicated comparing to the other solutions.
To answer your question in the comment, I would probably do something like that:
string MessageToUser = VaildateRadioGroupIsChecked(radioBttnAddrssCoYes, rdoBttnAddCorrectNo, "Check Addresss Correct") +
VaildateRadioGroupIsChecked(rdoGroundFloorTrue, radioGroundFFalse, "Please Check Ground Floor Delivery") +
VaildateRadioGroupIsChecked(rdoHomeTrue, radioMeet, "Please check Customer All day At Home");
if(MessageToUser.Length == 0) {
// do your stuff
} else {
Messagebox.Show(MessageToUser);
}
private string VaildateRadioGroupIsChecked(RadioButton a, RadioButton b, string MessageToUser)
{
if(!a.Checked && !b.Checked) {
return MessageToUser + "\n";
}
return string.Empty;
}
You could rewrite it to the following if you find it more readable and want to display custom messages to the User when some radios of a group are not checked.
if (! (rdoHomeTrue.Checked || radioMeet.Checked)) {
MessageBox.Show("Please check Customer All day At Home");
return;
}
if (! (rdoGroundFloorTrue.Checked || radioGroundFFalse.Checked)) {
MessageBox.Show("Please Check Ground Floor Delivery");
return;
}
if (! (radioBttnAddrssCoYes.Checked || rdoBttnAddCorrectNo.Checked)) {
MessageBox.Show("Check Addresss Correct");
return;
}
// here i do something

GKTurnBasedMatch handleMatchEnded: not called

I'm programming a simple 2 players board game with GameKit and the GKTurnBased API, my problem is at the end of my game, when I want to detect that a player won and another lost.
First at all, the handleTurnEventForMatch:didBecomeActive: method fired, in this method, I check the other participant status:
// Test other players status
for(GKTurnBasedParticipant *participant in match.participants)
{
if([participant.playerID isEqualToString:[GKLocalPlayer localPlayer].playerID])
continue;
if(participant.matchOutcome == GKTurnBasedMatchOutcomeQuit || participant.matchOutcome == GKTurnBasedMatchOutcomeLost)
{
[self findMeInMatch:match].matchOutcome = GKTurnBasedMatchOutcomeWon;
[match endMatchInTurnWithMatchData:match.matchData
completionHandler:nil];
return;
}
else if(participant.matchOutcome == GKTurnBasedMatchOutcomeWon)
{
[self findMeInMatch:match].matchOutcome = GKTurnBasedMatchOutcomeLost;
[match endMatchInTurnWithMatchData:match.matchData
completionHandler:nil];
return;
}
else if(participant.matchOutcome == GKTurnBasedMatchOutcomeTied)
{
[self findMeInMatch:match].matchOutcome = GKTurnBasedMatchOutcomeTied;
[match endMatchInTurnWithMatchData:match.matchData
completionHandler:nil];
return;
}
else
{
// OK
}
}
[...]
My problem now is that the handleMatchEnded: method is never called... The weird thing is that this method is part of the same delegate protocol as handleTurnEventForMatch:didBecomeActive: which actually works.
Any idea is welcome.
Cheers.
UPDATE 03/17:
As my first player quitted is turn, and the second one end the match, I think the two players are not going to receive the event. This event is probably made for other players in game, or when players are not within the app when the others leave. Didn't find anything related in the documentation, but it seem's logical.
Cyril

SharedSizeGroup Naming Rules

I have a question about naming SharedSizeGroups in WPF grids mostly out of curiosity. I noticed on MSDN that they list restrictions for the group's string name:
The SharedSizeGroup property value must satisfy the following rules:
Must not be empty.
Must only consist of letters, digits, and underscore characters.
Must not start with a numeric value.
I have some groups that I named numerically ("1", "2", "3", etc.) and have never had a problem with them. Just for kicks I renamed some groups to something like ",-[]" and they still worked too. So these rules are not enforced and seemingly not necessary. Does anybody know the reason for the rules in the documentation? Is it possible for the names to conflict with something that WPF is doing internally?
Edit: Okay, so WPF does enforce it after all, validation just doesn't fire in my non-compiled templates.
Interesting, I took a look at the DefinitionBase class in reflector and the SharedSizeGroup property.
It creates a dependency property with a validation callback defined as the following:
SharedSizeGroupProperty = DependencyProperty.Register("SharedSizeGroup", typeof(string), typeof(DefinitionBase), new FrameworkPropertyMetadata(new PropertyChangedCallback(DefinitionBase.OnSharedSizeGroupPropertyChanged)), new ValidateValueCallback(DefinitionBase.SharedSizeGroupPropertyValueValid));
private static bool SharedSizeGroupPropertyValueValid(object value)
{
if (value == null)
{
return true;
}
string str = (string)value;
if (str != string.Empty)
{
int num = -1;
while (++num < str.Length)
{
bool flag = char.IsDigit(str[num]);
if (((num == 0) && flag) || ((!flag && !char.IsLetter(str[num])) && ('_' != str[num])))
{
break;
}
}
if (num == str.Length)
{
return true;
}
}
return false;
}
I tested this, and it does in fact return false for anything containing non-numeric, non-alpha, non-underscore characters. It also returns false for any group starting with a number. So it seems to follow general variable name rules..
My guess is this would most likely throw some sort of exception, but perhaps it is being handled. Have you checked the output window?
I tried an invalid name, and I got an XAMLParseException.

Comparing chars

for my word game i'm comparing chars in words, 6 in the first word, 6 in the random word.
Now I want it to do things when certain letters are on there place.
for example if
(aChar1 == aChar7 && aChar2 == aChar8){
//do something
}
but later in my code there is
(aChar1 == aChar7 && aChar2 == aChar8 && aChar3 == aChar9){
//do something
}
Now I just want the second code line to happen and not the first since that is only if the first 2 are in place. I need to add code like :
(aChar1 == aChar7 && aChar2 == aChar8 && aChar3 isnotequalto aChar9){
//do something
}
What code should I use for saying not equal?
use this and tell me if it works
achar3!=achar9

Resources