Suppressing a beep winform / textbox - keydown

I have a key sequence "CTRL+U" and in the form I have a KeyDown method that looks at the key variable e and does what it wants. It does not set a result.
It works.
But if the focus on the form is in a text box and I press the key combination, it still works, but then I get the beep.
I am a little confused as to how to resolve this as it sounds a lot of work to have to suppress a key event in every control (should I have several text boxes).
The handler:
private void XXXXXForm_KeyDown(object sender, KeyEventArgs e)
{
if(e.Control)
{
bool bHandle = false;
bool bChecked = true;
if (e.KeyCode == Keys.U)
{
bChecked = false;
bHandle = true;
}
else if (e.KeyCode == Keys.T)
{
bChecked = true;
bHandle = true;
}
if(bHandle)
{
// Do stuff
}
}
}

I found this information on another website:
The "e.Handled = true;" statement is not doing what you think here.
The documentation [^]for this is confusing, and one could interpret it
the way you have. However, you need to realize that they are talking
about setting "Handled" in the KeyPress Event. To make matters worse,
the KeyPressed event uses KeyPressEventArgs not KeyEventArgs.
Instead use e.SuppressKeyPress = true;
That was the solution!

Related

How can I make a symbol appear after clicking in two different buttons in actionscript 3.0?

I am a little bit new in this programming stuff and I need help to program in ActionScript 3.0 (Adobe Animate CC). I want to make a symbol (graph) visible but only after clicking in two different buttons (button1 and button 2). I can make that with just one button, but I can't make it with two buttons... Can anyone help me? I tried this code but it isn't working as it should:
button1.addEventListener (MouseEvent.CLICK, fl_MouseClickHandler_1);
button2.addEventListener (MouseEvent.CLICK, fl_MouseClickHandler_1);
function fl_MouseClickHandler_1(event:MouseEvent):void
{
graph.visible = true;
}
Tom
Simplest way would be to do this, although there may be other ways to do it as well:
var isButton1Clicked:Boolean = false;
var isButton2Clicked:Boolean = false;
button1.addEventListener (MouseEvent.CLICK, fl_MouseClickHandler_1);
button2.addEventListener (MouseEvent.CLICK, fl_MouseClickHandler_1);
function fl_MouseClickHandler_1(event:MouseEvent):void
{
if (event.currentTarget == button2)
isButton2Clicked = true;
else if (event.currentTarget == button1)
isButton1Clicked = true;
if (isButton1Clicked && isButton2Clicked)
{
graph.visible = true;
isButton1Clicked = isButton2Clicked = false;
}
}
Note that I reset both the Boolean values to false, once the graph is visible so that it works like a reset.
On a side note, I would recommend to use better names for your buttons and your event handlers. Just best practice.
Hope this helps. Cheers.

After enabling spell checking with Squiggly the delete key and arrow keys stop working

After enabling Squiggly on a TextFlow the delete key and arrow keys stop working. If I click around or switch applications and switch back it sometimes starts to work again. If I do not use Squiggly at all the delete and arrow keys work as expected.
Here is the code I'm using to enable Squiggly:
var locale:String = "en_US";
SpellUIForTLF.enableSpelling(myTextFlow, locale);
Has anyone encountered this and is there a fix?
It looks like a bug with TLF or the SpellUIForTLF class. In SpellUIForTLF class there is a method, "doSpellingJob" that is called. It is after this call that you want reselect the selection in the textflow.
// From customized SpellUIForTLF.as class
public function doSpellingJob():void
{
if (_spellingEnabled == false) return;
hh.clearSquiggles();
for (var idx:int = 0; idx < mTextFlow.flowComposer.numControllers; idx++)
{
var testController:ContainerController = mTextFlow.flowComposer.getControllerAt(idx);
//if (getValidFirstWordIndexTLF(testController) != -1)
spellCheckRangeTLF(getValidFirstWordIndexTLF(testController), getValidLastWordIndexTLF(testController));
}
if (hasEventListener(Event.COMPLETE)) {
dispatchEvent(new Event(Event.COMPLETE));
}
if (keepFocusOnTextFlow && !calledSetFocusOnce) {
setFocusOnTextFlowContainer();
calledSetFocusOnce = true;
}
}
public function setFocusOnTextFlowContainer():void {
//trace("Setting focus on textflow");
var testController:ContainerController;
if (mTextFlow.flowComposer) {
if (mTextFlow.flowComposer.numControllers) {
testController = mTextFlow.flowComposer.getControllerAt(0);
}
}
FlexGlobals.topLevelApplication.stage.focus = null;
if (testController) {
FlexGlobals.topLevelApplication.stage.focus = testController.container;
}
if (mTextFlow.interactionManager) {
//mTextFlow.flowComposer.updateAllControllers();
mTextFlow.interactionManager.setFocus();
var selectionState:SelectionState = mTextFlow.interactionManager.getSelectionState();
//TextObject(currentObject).textFlow.interactionManager.clearSelection();
mTextFlow.interactionManager.selectRange(selectionState.anchorPosition, selectionState.activePosition);
}
}
/**
* Sets the focus on the first container controller on the first composition complete.
* This is to fix a bug in some situations where the delete and arrow keys do not
* work after spell checking has been enabled before the text flow composition
* has completed. Usually there is also a delay with editing the first time the dictionary loads.
* */
public static var keepFocusOnTextFlow:Boolean;
public static var calledSetFocusOnce:Boolean;
public function SpellUIForTLF(textModel:TextFlow, lang:String)
{
...
calledSetFocusOnce = false;
if (keepFocusOnTextFlow) {
setFocusOnTextFlowContainer();
}
...
}

How to remove if statement with an if statement? as3

I'm making a game where the character has to pass through the gate after getting the key
so when he gets the key the gate is suppose to go away, i used
if(character.hitTestObject(gate))
{character.visible = false;
youLose_text.visible = true; }
BUT when he gets the key: it's
if(character.hitTestObject(key))
{
gate.visible = false; }
NOW when i pass through the gate, i obviously get killed again
now how do i remove the previous if function through the next if function?
Just set/check a boolean. One already exists in gate.visible.
// If gate is visible and character hitTests gate then..
if(gate.visible && character.hitTestObject(gate)) {
character.visible = false;
youLose_text.visible = true;
}
If the gate is supposed to actually "go away", you should completely remove it, not just make it invisible... e.g.:
if(character.hitTestObject(key)) {
//don't forget to remove event listeners on the gate as well, if it had any
removeChild(gate);
gate = null;
} else if(gate != null) {
if(character.hitTestObject(gate)) {
character.visible = false;
youLose_text.visible = true;
}
}

Disable a function temporarily as3

On my screen are tiles generated from an array. I have a mouse roll over function called rollover, that adds a movieclip that highlights the edge of the tiles that I am currently on. I want it so that once I click a tile, the roll over function doesn't work until another button is clicked. I tried putting removeEventListener for the roll over function in the click function, doesn't seem to work. How would I go about this if possible?
I will post more information if needed.
function rollover(event:MouseEvent)
{
var tileHover = true;
if (tileHover == true){
(event.currentTarget as Tile).outline.gotoAndStop("hover");
}
if(tileHover == false){
(event.currentTarget as Tile).outline.gotoAndStop("blank");
}
}
Below is the mouseclick function
function mouseclick(event:MouseEvent)
{
tileHover = false;
if (tileHover == false){
tile_MC.removeEventListener(MouseEvent.ROLL_OVER, rollover)
}
}
See below. You set a property and immediately check what the value of that property is. It will always be true because you just set it as true.
var tileHover = true;
if (tileHover == true){
(event.currentTarget as Tile).outline.gotoAndStop("hover");
}
Also, don't forget your data types.
I think you need to have (event.currentTarget as Tile).outline.gotoAndStop("blank"); in mouseclick.
Also, I assume tilehover is some global variable used for tracking the hover state. Explicitly setting it true/false in the handler is just for debugging purposes!!

ActionScript MouseEvent's CLICK vs. DOUBLE_CLICK

is it not possible to have both CLICK and DOUBLE_CLICK on the same display object? i'm trying to have both for the stage where double clicking the stage adds a new object and clicking once on the stage deselects a selected object.
it appears that DOUBLE_CLICK will execute both itself as well as the first CLICK functions in the path toward DOUBLE CLICK (mouse down, mouse up, click, mouse down, mouse up, double click).
in other languages i've programmed with there was a built-in timers that set the two apart. is this not available in AS3?
UPDATE
here's some code. essentially what i would like is have one or the other, not both with double click
stage.doubleClickEnabled = true;
stage.addEventListener(MouseEvent.DOUBLE_CLICK, twoClicks, false, 0, true);
stage.addEventListener(MouseEvent.CLICK, oneClick, false, 0, true);
function oneClick(evt:MouseEvent):void
{
trace("One CLICK");
}
function twoClicks(evt:MouseEvent):void
{
trace("Two CLICKS");
}
//oneClick trace = "One CLICK"
//twoClicks trace = "One CLICK Two CLICKS" (instead of just Two CLICKS)
Well, you could use setTimeout and clearTimeout.
It'd look something like this:
const var DOUBLE_CLICK_SPEED:int = 10;
var mouseTimeout;
function handleClick(evt:MouseEvent):void {
if (mouseTimeout != undefined) {
twoClicks();
clearTimeout(mouseTimeout);
mouseTimeout = undefined;
} else {
function handleSingleClick():void {
oneClick();
mouseTimeout = undefined;
}
mouseTimeout = setTimeout(handleSingleClick, DOUBLE_CLICK_SPEED);
}
}
function oneClick(evt:MouseEvent):void {
trace("One CLICK");
}
function twoClicks(evt:MouseEvent):void {
trace("Two CLICKS");
}
stage.addEventListener(MouseEvent.CLICK, handleClick, false, 0, true);
Did you set .doubleClickEnabled to true?
You should also take a look here.
Great answer Wallacoloo - thanks for that. I have just implemented your solution and refined a few points, so I'd thought I'd put it here for future reference (and of course the benefit of the overflow community!). Firstly, I couldn't test for undefined on the uint returned by setTimeout, so I replaced the undefined conditional with an == 0 conditional. Secondly, I wanted to commit the logic of a single click instantaneously (just makes for a more pleasant user interface), so I've done a bit of reshuffling:
if (mouseTimeout != 0) {
// clicked within the timeout, handle as double click
// rollback single click logic
rollbackSingleClickHandler(e);
// commit double click logic
dblClickHandler(e);
clearTimeOut(mouseTimeout);
mouseTimeout = 0;
} else {
// first click of a click sequence
// commit single click logic
singleClickHandler(e);
function clearTime():void {
mouseTimeout = 0;
}
// register a timeout for a potential double click
mouseTimeout = setTimeout(clearTime, DOUBLE_CLICK_SPEED);
}