|
5631
|
Programación / Programación General / Re: Sintaxis REGEXP para sustituir una palabra x otra
|
en: 19 Marzo 2015, 16:01 pm
|
Te pongo un breve ejemplo: Tienes esta cadena de texto: Tienes esta expresión regular: Cada pareja de paréntesis representa un grupo, cada grupo es una especie de delimitador de captura, y a la expresión debes añadirle otra pareja de paréntesis imaginaria de esta manera: " ((.+)(\,)(\s+)(.+)(\!))" que representa el primer grupo de todos (es decir la captura completa), por ende: Grupo 0: "Hola, Mundo!" Grupo 1: "Hola" Grupo 2: "," Grupo 3: " " Grupo 4: "Mundo" Grupo 5: "!" Conociendo lo que hemos capturado, los grupos de los que consta la captura, el índice de los grupos, y dando por hecho que se utilice el símbolo " $" para interpretar un grupo, entonces solo debemos inercambiar los grupos en el orden deseado, por ejemplo, la unión de los grupos "$4$2$3$1$5" o también "Mundo, $1!" darían cómo resultado:
El programa o lenguaje que estés utilizando debe tener funcionalidades de reemplazado (o replace) cómo te ha comentado el compañero Engel Lex. Cabe mencionar que existen diferencias de sintaxis del motor Regex puede dependiendo del lenguaje en el que se utilice, por ejemplo te puedes encontrar que los grupos se representan con un símbolo/sintaxis diferente en otro lenguaje de scripting, además de eso, en lenguajes orientados a objetos, el manejo de los grupos se suele hacer mediante el uso de miembros (classes y propiedades), en lugar de interpretar símbolos cómo "$0". Lee las documentación de la Wikipedia, que muy pocas fuentes superan esta información: Regular expression - Wikipedia, the free encyclopedia Por último, te muestro un ejemplo en VB.Net (en C# sería lo mismo): Imports System.Text.RegularExpressions Public NotInheritable Class Form1 Dim input As String = "Hola, Mundo!" Dim format As String Dim replace As String Dim regEx As New Regex("(.+)(\,)(\s+)(.+)(\!)", RegexOptions.IgnoreCase) Private Sub Test() Handles MyBase.Shown Dim matches As MatchCollection = regEx.Matches(input) Dim value1 As String = matches(0).Groups(1).Value ' "Hola" Dim value2 As String = matches(0).Groups(2).Value ' "," Dim value3 As String = matches(0).Groups(3).Value ' " " Dim value4 As String = matches(0).Groups(4).Value ' "Mundo" Dim value5 As String = matches(0).Groups(5).Value ' "!" format = String.Format("{0}{1}{2}{3}{4}", value4, value2, value3, value1, value5) replace = input.Replace(value1, value4). Replace(value3 & value4, value3 & value1) End Sub End Class
Saludos
|
|
|
5633
|
Programación / .NET (C#, VB.NET, ASP) / Re: ¿Hay repercusiones al cambiar hora al sistema?
|
en: 18 Marzo 2015, 20:21 pm
|
para ello importo la biblioteca kernel32. No necesitas hacer P/Invoking a ninguna función de la API de Windows, puedes recurrir al NamesPace Microsoft.VisualBasic para modificar la fecha y hora del SO: Dim d As New Date(year:=2000, month:=1, day:=1, hour:=0, minute:=0, second:=0) Microsoft.VisualBasic.TimeOfDay = d Microsoft.VisualBasic.DateString = d.ToString("MM/dd/yyyy")
mi pregunta es si para el sistema operativo traerá alguna repercusión ejecutar mi aplicación digamos que cada segundo adelante 30 segundos y esté así por 3 minutos? o no representaría ningún problema. No representa ningún problema mientras el margen sea pequeño y/o la fecha esté dentro del rango de fechas permitido. Márgenes demasiado largos podrían surgir problemas derivados cómo: • Aplicaciones triales que caducan, ya que muchas aplicaciones que basan su protección en la hora interna del SO les afecta el cambio (a otras aplicaciones con algoritmos de protección más sofisticados no les afecta el cambio). • Alteraciones en el comportamiento de ciertas aplicaciones hasta volverse inestables y en casos extremos dejen de funcionar por completo. Cómo por ejemplo aplicaciones de calendarios o tareas programadas que dependan de la fecha y hora real, o una aplicación que revise actualizaciones de si misma cada 7 días, etc... • Seguramente otro tipo de problemas, cómo no saber en el día que estás viviendo . Saludos
|
|
|
5634
|
Sistemas Operativos / Windows / Re: ¿son importantes estos programas de inicio?
|
en: 18 Marzo 2015, 07:44 am
|
El nombre del primer proceso define su funcionalidad por si mismo, o al menos nos podemos hacer una idea, es una aplicación que controla/administra los eventos que son disparados por los dispositivos de Epson que tengas instalados, por lo tanto, ¿será importante?, pues al menos un poquito si que lo será.
El segundo tiene que ver con la salida de audio de Realtek, otro componente de hardware de tu equipo, y el tercero lo mismo, de Epson.
El resto puedes deshabilitarlos sin problemas, los servicios adicionales de nVidia y todo lo relacionado con MS Office no son importantes, y mucho menos el del reproductor PowerDVD.
Saludos
|
|
|
5637
|
Programación / Java / Re: [Duda]Es posible detectar teclas si mi aplicacion no tiene el foco
|
en: 17 Marzo 2015, 06:19 am
|
Muchas gracias por el aporte. Si bien estoy viendo, eso es un código hecho en C++ y si no me equivoco tiene que ver con el uso de eventos de teclado, pero la duda viene ahora: Lo que te ha mostrado el compañero @[ Thunder | CLS ] es la referencia de la función RegisterHotKey de la API de Windows, arriba de la página te muestra la firma de la función y abajo un ejemplo de uso en C++. Nota: Y la función UnregisterHotKey realiza lo opuesto. Y otra cosa ¿Como es posible hacer librerías con C y C++ para poder usarlas en Java? Antes de que sigas leyendo esto, te aconsejo que esperes nuevas respuestas al tema para que puedas hacerlo directamente en Java, te costará mucho menos esfuerzo, sigue leyendo lo que te propongo solamente si te interesa experimentar técnicas alternativas, aunque ya te aviso que esto resultaría mucho más complejo, si no te apetece puedes omitir toda esta parrafada que he escrito abajo .... Doy por hecho que no te importaría utilizar librerías de terceros en tu proyecto de Java, así que te presento un código que desarrollé hace tiempo para administrar los atajos globales del teclado (o system-wide hotkeys), el código está escrito en VB.Net, antes de seguir explicando te muestro un ejemplo de uso en puro código .Net: Public Class Form1 ''' <summary> ''' Define the system-wide hotkey object. ''' </summary> Private WithEvents hotkey As HotkeyManager ''' <summary> ''' Initializes a new instance of this class. ''' </summary> Public Sub Test() Handles MyBase.Shown MyClass.InitializeComponent() ' Registers a new global hotkey on the system. (Alt + Ctrl + A) hotkey = New HotkeyManager(HotkeyManager.KeyModifier.Alt Or HotkeyManager.KeyModifier.Ctrl, Keys.A) ' Replaces the current registered hotkey with a new one. (Alt + Escape) hotkey = New HotkeyManager(DirectCast([Enum].Parse(GetType(HotkeyManager.KeyModifier), "Alt", True), HotkeyManager.KeyModifier), DirectCast([Enum].Parse(GetType(Keys), "Escape", True), Keys)) ' Set the tag property. hotkey.Tag = "I'm a String tag" End Sub ''' <summary> ''' Handles the Press event of the HotKey object. ''' </summary> Private Sub HotKey_Press(ByVal sender As Object, ByVal e As HotkeyManager.HotKeyEventArgs) _ Handles hotkey.Press Dim sb As New System.Text.StringBuilder With sb .AppendLine(String.Format("Key.......: {0}", e.Key.ToString)) .AppendLine(String.Format("Modifier..: {0}", e.Modifier.ToString)) .AppendLine(String.Format("Identifier: {0}", e.Id)) .AppendLine(String.Format("Press-count: {0}", e.Count)) .AppendLine(String.Format("Tag: {0}", DirectCast(sender, HotkeyManager).Tag.ToString)) End With MessageBox.Show(sb.ToString, String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information) ' Unregister the hotkey. Me.hotkey.Unregister() ' Register it again. Me.hotkey.Register() ' Is Registered? Debug. WriteLine(Me. hotkey. IsRegistered) End Sub End Class
Y aquí el código fuente: ' *********************************************************************** ' Author : Elektro ' Modified : 17-March-2015 ' *********************************************************************** ' <copyright file="HotkeyManager.vb" company="Elektro Studios"> ' Copyright (c) Elektro Studios. All rights reserved. ' </copyright> ' ***********************************************************************
#Region " Option Statements "
Option Explicit On Option Strict On Option Infer Off
#End Region
#Region " Imports "
Imports System.ComponentModel Imports System.Runtime.InteropServices
#End Region
#Region " Global Hotkey "
''' <summary> ''' Manages a system-wide hotkey. ''' </summary> Public NotInheritable Class HotkeyManager : Inherits NativeWindow : Implements IDisposable
#Region " P/Invoke "
''' <summary> ''' Platform Invocation methods (P/Invoke), access unmanaged code. ''' This class does not suppress stack walks for unmanaged code permission. ''' <see cref="System.Security.SuppressUnmanagedCodeSecurityAttribute"/> must not be applied to this class. ''' This class is for methods that can be used anywhere because a stack walk will be performed. ''' MSDN Documentation: http://msdn.microsoft.com/en-us/library/ms182161.aspx ''' </summary> Private NotInheritable Class NativeMethods
''' <summary> ''' Defines a system-wide hotkey. ''' </summary> ''' <param name="hWnd">The hWND.</param> ''' <param name="id">The identifier of the hotkey. ''' If the hWnd parameter is NULL, then the hotkey is associated with the current thread rather than with a particular window. ''' If a hotkey already exists with the same hWnd and id parameters.</param> ''' <param name="fsModifiers">The keys that must be pressed in combination with the key specified by the uVirtKey parameter ''' in order to generate the WM_HOTKEY message. ''' The fsModifiers parameter can be a combination of the following values.</param> ''' <param name="vk">The virtual-key code of the hotkey.</param> ''' <returns> ''' <c>true</c> if the function succeeds, otherwise <c>false</c> ''' </returns> <DllImport("user32.dll", SetLastError:=True)> Public Shared Function RegisterHotKey( ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As UInteger, ByVal vk As UInteger ) As <MarshalAs(UnmanagedType.Bool)> Boolean End Function
''' <summary> ''' Unregisters a hotkey previously registered. ''' </summary> ''' <param name="hWnd">The hWND.</param> ''' <param name="id">The identifier of the hotkey to be unregistered.</param> ''' <returns> ''' <c>true</c> if the function succeeds, otherwise <c>false</c> ''' </returns> <DllImport("user32.dll", SetLastError:=True)> Public Shared Function UnregisterHotKey( ByVal hWnd As IntPtr, ByVal id As Integer ) As <MarshalAs(UnmanagedType.Bool)> Boolean End Function
End Class
#End Region
#Region " Properties "
''' <summary> ''' Gets the key assigned to the hotkey. ''' </summary> ''' <value>The key assigned to the hotkey.</value> Public ReadOnly Property Key As Keys Get Return Me.pressEventArgs.Key End Get End Property
''' <summary> ''' Gets the key-modifier assigned to the hotkey. ''' </summary> ''' <value>The key-modifier assigned to the hotkey.</value> Public ReadOnly Property Modifier As KeyModifier Get Return Me.pressEventArgs.Modifier End Get End Property
''' <summary> ''' Gets the unique identifier assigned to the hotkey. ''' </summary> ''' <value>The unique identifier assigned to the hotkey.</value> Public ReadOnly Property ID As Integer Get Return Me.pressEventArgs.Id End Get End Property
''' <summary> ''' Gets or sets the user-defined data associated with this object. ''' </summary> ''' <value>The user-defined data associated with this object.</value> Public Property Tag As Object
''' <summary> ''' Gets a value that specifies how many times was pressed the hotkey. ''' </summary> ''' <value>A value that specifies how many times was pressed the hotkey.</value> Public ReadOnly Property Count As Integer Get Return Me.count1 End Get End Property ''' <summary> ''' A value that specifies how many times was pressed the hotkey. ''' </summary> Private count1 As Integer
#End Region
#Region " Enumerations "
''' <summary> ''' Specifies the avaliable key-modifiers to assign for a hotkey. ''' </summary> <FlagsAttribute> Public Enum KeyModifier As Integer
''' <summary> ''' Any modifier. ''' </summary> None = &H0
''' <summary> ''' The Alt key. ''' </summary> Alt = &H1
''' <summary> ''' The Control key. ''' </summary> Ctrl = &H2
''' <summary> ''' The Shift key. ''' </summary> Shift = &H4
''' <summary> ''' The Windows key. ''' </summary> Win = &H8
End Enum
''' <summary> ''' Specifies A Windows Message Identifier. ''' </summary> Private Enum WindowsMessages As Integer
''' <summary> ''' Posted when the user presses a hot key registered by the RegisterHotKey function. ''' The message is placed at the top of the message queue associated with the thread that registered the hot key. ''' <paramref name="WParam"/> ''' The identifier of the hot key that generated the message. ''' If the message was generated by a system-defined hot key. ''' <paramref name="LParam"/> ''' The low-order word specifies the keys that were to be pressed in ''' combination with the key specified by the high-order word to generate the WM_HOTKEY message. ''' </summary> WM_HOTKEY = &H312
End Enum
#End Region
#Region " Events "
''' <summary> ''' Event that is raised when a hotkey is pressed. ''' </summary> Public Event Press As EventHandler(Of HotKeyEventArgs)
''' <summary> ''' Stores the Press Event Arguments. ''' </summary> Private ReadOnly pressEventArgs As HotKeyEventArgs
''' <summary> ''' Event arguments for the Press event. ''' </summary> Public NotInheritable Class HotKeyEventArgs : Inherits EventArgs
''' <summary> ''' Gets or sets the key assigned to the hotkey. ''' </summary> ''' <value>The key assigned to the hotkey.</value> Public Property Key As Keys
''' <summary> ''' Gets or sets the key-modifier assigned to the hotkey. ''' </summary> ''' <value>The key-modifier assigned to the hotkey.</value> Public Property Modifier As KeyModifier
''' <summary> ''' Gets or sets the unique identifier assigned to the hotkey. ''' </summary> ''' <value>The unique identifier assigned to the hotkey.</value> Public Property Id As Integer
''' <summary> ''' Gets or sets how many times was pressed the hotkey. ''' </summary> Public Property Count As Integer
End Class
#End Region
#Region " Exceptions "
''' <summary> ''' Exception that is thrown when a hotkey tries to register but is already registered. ''' </summary> <Serializable> Private NotInheritable Class IsRegisteredException : Inherits Exception
''' <summary> ''' Initializes a new instance of the <see cref="IsRegisteredException"/> class. ''' </summary> Sub New() MyBase.New("Unable to register because the hotkey is already registered.") End Sub
End Class
''' <summary> ''' Exception that is thrown when a hotkey tries to unregister but is not registered. ''' </summary> <Serializable> Private NotInheritable Class IsNotRegisteredException : Inherits Exception
''' <summary> ''' Initializes a new instance of the <see cref="IsNotRegisteredException"/> class. ''' </summary> Sub New() MyBase.New("Unable to unregister because the hotkey is not registered.") End Sub
End Class
#End Region
#Region " Constructors "
''' <summary> ''' Creates a new system-wide hotkey. ''' </summary> ''' <param name="Modifier"> ''' Indicates the key-modifier to assign to the hotkey. ''' ( Can use one or more modifiers ) ''' </param> ''' <param name="Key"> ''' Indicates the key to assign to the hotkey. ''' </param> ''' <exception cref="IsRegisteredException"></exception> <DebuggerStepperBoundary()> Public Sub New(ByVal modifier As KeyModifier, ByVal key As Keys)
MyBase.CreateHandle(New CreateParams)
Me.pressEventArgs = New HotKeyEventArgs With Me.pressEventArgs .Id = MyBase.GetHashCode() .Key = key .Modifier = modifier .Count = 0 End With
If Not NativeMethods.RegisterHotKey(MyBase.Handle, Me.ID, CUInt(Me.Modifier), CUInt(Me.Key)) Then
Throw New IsRegisteredException
End If
End Sub
#End Region
#Region " Event-Handlers "
''' <summary> ''' Occurs when a hotkey is pressed. ''' </summary> Private Sub OnHotkeyPress() Handles Me.Press Me.count1 += 1 End Sub
#End Region
#Region " Public Methods "
''' <summary> ''' Determines whether this hotkey is registered on the system. ''' </summary> ''' <returns> ''' <c>true</c> if this hotkey is registered; otherwise, <c>false</c>. ''' </returns> Public Function IsRegistered() As Boolean
Me.DisposedCheck()
' Try to unregister the hotkey. Select Case NativeMethods.UnregisterHotKey(MyBase.Handle, Me.ID)
Case False ' Unregistration failed. Return False ' Hotkey is not registered.
Case Else ' Unregistration succeeds. Me.Register() ' Re-Register the hotkey before return. Return True ' Hotkey is registeres.
End Select
End Function
''' <summary> ''' Registers this hotkey on the system. ''' </summary> ''' <exception cref="IsRegisteredException"></exception> Public Sub Register()
Me.DisposedCheck()
If Not NativeMethods.RegisterHotKey(MyBase.Handle, Me.ID, CUInt(Me.Modifier), CUInt(Me.Key)) Then
Throw New IsRegisteredException
End If
End Sub
''' <summary> ''' Unregisters this hotkey from the system. ''' After calling this method the hotkey turns unavaliable. ''' </summary> ''' <param name="resetCounter">If set to <c>true</c>, resets the htkey press count.</param> ''' <exception cref="WindowsApplication2.HotkeyManager.IsNotRegisteredException"></exception> Public Sub Unregister(Optional ByVal resetCounter As Boolean = False)
Me.DisposedCheck()
If Not NativeMethods.UnregisterHotKey(MyBase.Handle, Me.ID) Then Throw New IsNotRegisteredException End If
If resetCounter Then Me.pressEventArgs.Count = 0 End If
End Sub
#End Region
#Region " Hidden methods "
''' <summary> ''' Assigns a handle to this window. ''' </summary> <EditorBrowsable(EditorBrowsableState.Never)> Public Shadows Sub AssignHandle(handle As IntPtr) MyBase.AssignHandle(handle) End Sub
''' <summary> ''' Creates a window and its handle with the specified creation parameters. ''' </summary> <EditorBrowsable(EditorBrowsableState.Never)> Public Shadows Sub CreateHandle(cp As CreateParams) MyBase.CreateHandle(cp) End Sub
''' <summary> ''' Destroys the window and its handle. ''' </summary> <EditorBrowsable(EditorBrowsableState.Never)> Public Shadows Sub DestroyHandle() MyBase.DestroyHandle() End Sub
''' <summary> ''' Releases the handle associated with this window. ''' </summary> <EditorBrowsable(EditorBrowsableState.Never)> Public Shadows Sub ReleaseHandle() MyBase.ReleaseHandle() End Sub
''' <summary> ''' Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. ''' </summary> <EditorBrowsable(EditorBrowsableState.Never)> Public Shadows Function CreateObjRef(requestedType As Type) As System.Runtime.Remoting.ObjRef Return MyBase.CreateObjRef(requestedType) End Function
''' <summary> ''' Invokes the default window procedure associated with this window. ''' </summary> <EditorBrowsable(EditorBrowsableState.Never)> Public Shadows Sub DefWndProc(ByRef m As Message) MyBase.DefWndProc(m) End Sub
''' <summary> ''' Retrieves the current lifetime service object that controls the lifetime policy for this instance. ''' </summary> <EditorBrowsable(EditorBrowsableState.Never)> Public Shadows Function GetLifeTimeService() As Object Return MyBase.GetLifetimeService End Function
''' <summary> ''' Obtains a lifetime service object to control the lifetime policy for this instance. ''' </summary> <EditorBrowsable(EditorBrowsableState.Never)> Public Shadows Function InitializeLifeTimeService() As Object Return MyBase.InitializeLifetimeService End Function
''' <summary> ''' Serves as a hash function for a particular type. ''' </summary> <EditorBrowsable(EditorBrowsableState.Never)> Public Shadows Function GetHashCode() As Integer Return MyBase.GetHashCode End Function
''' <summary> ''' Gets the System.Type of the current instance. ''' </summary> ''' <returns>The exact runtime type of the current instance.</returns> <EditorBrowsable(EditorBrowsableState.Never)> Public Shadows Function [GetType]() As Type Return MyBase.GetType End Function
''' <summary> ''' Determines whether the specified System.Object instances are considered equal. ''' </summary> <EditorBrowsable(EditorBrowsableState.Never)> Public Shadows Function Equals(ByVal obj As Object) As Boolean Return MyBase.Equals(obj) End Function
''' <summary> ''' Returns a String that represents the current object. ''' </summary> <EditorBrowsable(EditorBrowsableState.Never)> Public Shadows Function ToString() As String Return MyBase.ToString End Function
''' <summary> ''' Gets the handle for this window. ''' </summary> <EditorBrowsable(EditorBrowsableState.Never)> Public Shadows Property Handle() As IntPtr
#End Region
#Region " WndProc "
''' <summary> ''' Invokes the default window procedure associated with this window to process messages. ''' </summary> ''' <param name="m"> ''' A <see cref="T:System.Windows.Forms.Message"/> that is associated with the current windows message. ''' </param> Protected Overrides Sub WndProc(ByRef m As Message)
Select Case m.Msg
Case WindowsMessages.WM_HOTKEY ' A hotkey is pressed.
' Update the pressed counter. Me.pressEventArgs.Count += 1
' Raise the Event RaiseEvent Press(sender:=Me, e:=Me.pressEventArgs)
Case Else MyBase.WndProc(m)
End Select
End Sub
#End Region
#Region " IDisposable "
''' <summary> ''' To detect redundant calls when disposing. ''' </summary> Private isDisposed As Boolean = False
''' <summary> ''' Prevent calls to methods after disposing. ''' </summary> ''' <exception cref="System.ObjectDisposedException"></exception> Private Sub DisposedCheck()
If Me.isDisposed Then Throw New ObjectDisposedException(Me.GetType().FullName) End If
End Sub
''' <summary> ''' Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. ''' </summary> Public Sub Dispose() Implements IDisposable.Dispose Me.Dispose(isDisposing:=True) GC.SuppressFinalize(Me) End Sub
''' <summary> ''' Releases unmanaged and - optionally - managed resources. ''' </summary> ''' <param name="IsDisposing"> ''' <c>true</c> to release both managed and unmanaged resources; ''' <c>false</c> to release only unmanaged resources. ''' </param> Protected Sub Dispose(ByVal isDisposing As Boolean)
If Not Me.isDisposed Then
If isDisposing Then NativeMethods.UnregisterHotKey(MyBase.Handle, Me.ID) End If
End If
Me.isDisposed = True
End Sub
#End Region
End Class
#End Region Bien, la idea sería que compilases el código a una librería.dll (lo puedes hacer con Visual Studio, creando un proyecto de tipo ' Class Library' y copiando el source de mi código) Una vez tengas compilado el ensamblado .Net (librería.dll), solo tienes que utilizar alguna herramienta cómo JNA, JNI, o JavoNet para poder utilizarla desde tu proyecto Java. Lo cierto es que yo no se nada de Java, pero si te ha gustado la idea entonces te intentaré orientar cómo pueda... Se dice que JNA es mejor y más sencillo de implementar que JNI, así que tomemos cómo referencia JNA, lo que debes hacer (según explican aquí) es crear una interface que servirá cómo wrapper de la librería .net, tal que así (esto es solo un ejemplo): public interface NativeExample{ public int method1 (String param1 ); public boolean mehthod2(); }
Y así para importar la librería .net: NativeExample nativeExample = (NativeExample) Native.loadLibrary("librería", NativeExample.class);
Bien, si te fijas, la funcionalidad principal de la class desarrollada en .Net la expongo a traves de un constructor (o ctor): ' Registers a new global hotkey on the system. (Alt + Ctrl + A) hotkey = New HotkeyManager(HotkeyManager.KeyModifier.Alt Or HotkeyManager.KeyModifier.Ctrl, Keys.A)
En la interface de Java debes especificar la misma parametización que utiliza el ctor de mi Class. El método New acepta dos parámetros de tipo Integer (o Int32), el primer parámetro es un valor de esta enumeración del código fuente, que especifica un modificador de tecla: ''' <summary> ''' Specifies the avaliable key-modifiers to assign for a hotkey. ''' </summary> <FlagsAttribute> Public Enum KeyModifier As Integer ''' <summary> ''' Any modifier. ''' </summary> None = &H0 ''' <summary> ''' The Alt key. ''' </summary> Alt = &H1 ''' <summary> ''' The Control key. ''' </summary> Ctrl = &H2 ''' <summary> ''' The Shift key. ''' </summary> Shift = &H4 ''' <summary> ''' The Windows key. ''' </summary> Win = &H8 End Enum
Nota: Puedes combinar los valores de la enumeración, por ejemplo 4+2 = Shift+Control El segundo parámetro sería un valor de la enumeración System.Windows.Forms.Keys de la librería de clases de .Net Framework ( documentación: http://msdn.microsoft.com/de-de/library/system.windows.forms.keys%28v=vs.110%29.aspx ), con este pequeño código en VB.Net sacamos los valores Int32: For Each value As Integer In [Enum].GetValues(GetType(Keys)) Debug. WriteLine(String. Format("key: {0}", [Enum]. GetName(GetType(Keys ), value ))) Debug. WriteLine(String. Format("val: {0}", value )) Debug. WriteLine(String. Empty) Next value
Output: http://pastebin.com/UyTEVAQvCon esa implementación ya habrías conseguido registrar el Hotkey desde esta libreria .Net, pero aun faltaría lo más importante, asociar el hotkey a una acción determinada. En mi Class expongo el evento HotkeyManager.Press que se dispara al interceptar el mensaje de ventana WM_HOTKEY (documentación: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646279%28v=vs.85%29.aspx) y se puede handlear a traves de la instancia del objeto, como viste en este ejemplo: ''' <summary> ''' Handles the Press event of the HotKeyManager instance. ''' </summary> Private Sub HotKey_Press(ByVal sender As Object, ByVal e As HotkeyManager.HotKeyEventArgs) _ Handles hotkey.Press End Sub
Las instrucciones del interior de ese bloque de código se procesarían cuando se presione el hotkey que haya sido registrado en el sistema, y hasta aquí es donde puedo intentar orientarte, por que no tengo ni idea de cómo exponer eventos definidos en una clase de .Net a traves de Java (ni la firma del evento), deberías preguntar y/o documentarte por ti mismo. Un saludo!
|
|
|
5638
|
Foros Generales / Foro Libre / Re: EL VERDADERO FIN DEL MUNDO, Descubren 300 escapes masivos de metano en el Ártico
|
en: 17 Marzo 2015, 02:38 am
|
Esto es MUUUY hipócrita. Hablas de no creer a la tele y demás, pero tú eres el primero en defender a los conspiradores, como el JL Camacho (¿era ese el nombre?) ese, que hablaba de reptiles y no sé qué mierdas más.
Sin acritud.
Yo no defiendo a los consparanoicos, defiendo a ALGUNAS de sus hipótesis y/o teorías las cuales para mi tienen un argumento mucho más sostenible que lo que pueda salir por la boca de un político o de un presidente, pues tengo la capacidad mental cómo para estar abierto a la ÍNFIMA posibilidad de que se esté engañando a la sociedad en muchos sentidos, en muchas cosas, y muchas de ellas serían demasiado importantes como para obviarlas. Que un tio hable sobre gilipolleces cómo una Tierra hueca (por ejemplo, yo lo considero una tontería) no significa que yo crea en todas las gilipolleces que esa persona va diciendo, y además, ¿tú te libras de haber hablado sobre tonterías en alguna que otra ocasión a lo largo de tu vida?, ¿nunca has tenido una idea que hayas querido difundir y luego te has equivocado?, revisa a ver quien es el hipócrita, porque amigo, todos somos Humanos. PD: Me gustaría aclarar que no ignoré tu comentario, simplemente no me di cuenta hasta ahora, lo siento. Un saludo!
|
|
|
5639
|
Programación / Programación General / Re: Interceptar aplicacion en el momento en el que empieza
|
en: 17 Marzo 2015, 02:00 am
|
me gustaria que mi programa este a la espera hasta que un proceso determinado es llamado, una vez detectado poder modificar los argumentos que se le dan, si es que se le da alguno ¿En que lenguaje de programación lo piensas llevar a cabo?, no somos adivinos... La tarea que pides sería extremadamente fácil de llevar a cabo con los métodos y/o componentes del lenguaje en cuestión si no fuese por que además de interceptar la llamada quieres modificar los argumentos, y eso implica técnicas de hooking, o mejor dicho API Hooking, debes hookear la pre-llamada de la función "CreateProcess" de la librería "Kernel32.dll" de la API de Windows, en la pre-llamada a la función debes traducir/interpretar los parámetros enviados por esta función y modificarlos, donde cómo se explica la documentación oficial de la MSDN, el parámetro ' lpCommandLine' (de tipo String) contiene los argumentos commandline: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspxNota: El primer argumento (o argumento 0) siempre debe ser la ruta del proceso. ( Quizás debas hookear también la función 'ShellExecute' de la librería 'shell32.dll' para controlar la ejecución de un proceso en ciertas circunstancias ) Existen librerías para facilitar el enrutamiento de las funciones de la API de Windows, cómo por ejemplo Microsoft Detours para C/C++, y EasyHook o Nektra Deviare para C#/VB.Net; pero si piensas llevar a cabo la tarea sin ayuda de una librería de terceros entonces te advierto que se convertirá en una tarea horriblemente laboriosa y tediosa, y, por ejemplo en lo referente a la programación .Net requerirás un nivel medio-avanzado de conocimientos sobre el P/Invoking y el hooking, cómo Delegados, Callbacks, Marshalling, e indiferentemente del lenguaje que sea tampoco etsaría demás saber utilizar técnicas de programación asíncrónicas. A continuación te muestro un ejempo que he desarrollado en Vb.Net utilizando la librería Deviare, el siguiente código es un hook que se adjunta a las instancias activas del proceso "explorer.exe" y a sus procesos hijos para interceptar las llamadas a la función "CreateProcess" realizadas desde dichos procesos, si el nombre del proceso es "Notepad.exe" entonces modifico los argumentos del notepad para indicarle que cargue un archivo de texto ("C:\File.txt"). Nota: No es necesario interceptar la post-llamada de la función, pero deberías hacerlo para comprobar si hubo un error evaluando el valor de retorno de la función 'GetLastError' de la librería 'kernel32.dll" (si lo piensas hacer desde .Net entonces debes comprobar el valor de la función GetLastWin32Error de la Class ' Marshal') Nota 2: El código de abajo es solo un ejemplo, el hook no es persistente, es decir, si reinicias el proceso Explorer.exe dejará de funcionar. ' Nektra Deviare: "CreateProcess" hook example, By Elektro. Imports Nektra.Deviare2 Imports System.IO Public NotInheritable Class Form1 Public WithEvents SpyMgr As NktSpyMgr Public Hook As NktHook ReadOnly processesToAttach As IEnumerable(Of Process) = Process.GetProcessesByName("explorer") '.Concat(Process.GetProcessesByName("cmd")) ReadOnly processToIntercept As String = "notepad.exe" ReadOnly processArgs As String = "C:\File.txt" ' Notepad will try to load that filepath. ' "CreateProcess" API reference: ' http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspx Dim libName As String = "kernel32.dll" Dim funcName As String = "CreateProcessW" ' Unicode Dim hookFlags As eNktHookFlags = eNktHookFlags.flgOnlyPreCall Or eNktHookFlags.flgAutoHookChildProcess Private Sub Test() Handles MyBase.Load Me.SpyMgr = New NktSpyMgr() Me.SpyMgr.Initialize() Me.Hook = SpyMgr.CreateHook(String.Format("{0}!{1}", libName, funcName), hookFlags) Me.Hook.Hook(sync:=True) For Each proc As Process In processesToAttach Me.Hook.Attach(procOrId:=proc.Id, sync:=True) Next proc End Sub <MTAThread> Private Sub OnCreateProcess_Called(ByVal hook As NktHook, ByVal proc As NktProcess, ByVal callInfo As NktHookCallInfo) Handles SpyMgr.OnFunctionCalled Dim lpApplicationNameParam As NktParam = DirectCast(callInfo.Params(0), NktParam) Dim lpCommandLineParam As NktParam = DirectCast(callInfo.Params(1), NktParam) If Path.GetFileName(lpApplicationNameParam.Value.ToString).Equals(processToIntercept, StringComparison.OrdinalIgnoreCase) Then lpCommandLineParam.Value = String.Format("""{0}"" ""{1}""", lpApplicationNameParam.Value.ToString, Me.processArgs) End If End Sub End Class
Y, si lo que quieres es evitar que un proceso invocado mediante la función "CreateProcess" se inicie, entonces debes modificar el valor de retorno (result) de la función y despues saltar la llamada, con Deviare puedes hacerlo así: Private Sub OnCreateProcess_Called(ByVal hook As NktHook, ByVal proc As NktProcess, ByVal callInfo As NktHookCallInfo) Handles SpyMgr.OnFunctionCalled With callInfo If .IsPreCall Then ' Skip precall. Avoid process creation. .Result.Value = 1 .SkipCall() End If End With End Sub
Nota: Cada función de la API de Windows tiene sus valores de retorno y debes documentarte primero para saber cómo va a afectar al SO el valor de retorno que utilices. Saludos.
|
|
|
|
|
|
|